這次帶給大家python3與JS有什麼不同,使用python3與JS的注意事項有哪些,下面就是實戰案例,一起來看一下。
0.註解與程式碼區塊
JavaScript: //单行注释/* * 多行 * 注释 */ python: #单行注释'''多行注释'''
行與縮排
學習Python與其他語言最大的差別就是,Python的程式碼區塊不使用大括號({})來控制類,函數以及其他邏輯判斷。 python最具特色的就是用縮寫來寫模組。
1.變數
變數宣告與賦值
JavaScript: //变量声明赋值var a = "变量a";var A = "变量A";console.log(a);console.log(A);//多个变量赋值var a = "变量a", A = "变量A";console.log(a, A); python: #变量声明赋值a = "变量a";A = "变量A";print(a);print(A);#多个变量赋值a,A = "变量a", "变量A";print(a, A);
變數交換
JavaScript: var b = 1, c = 2;console.log(b, c);[b, c] = [c, b]console.log(b, c); python: b,c=1,2print(b,c);b,c=c,bprint(b,c);
常用變數型別
JavaScript: //typeof(??)<--用来查看类型console.log(typeof(1))console.log(typeof(1.0))console.log(typeof('a'))console.log(typeof('aaaa'))console.log(typeof([]))console.log(typeof({})) python: #type(??)<--用来查看类型print(type(1))print(type(1.0))print(type('a'))print(type('aaaa'))print(type([]))print(type({}))
常用變數型別轉換
JavaScript: console.log(typeof((1).toString()), "转为字符串类型")console.log(typeof(parseInt("123")), "转为数字类型")console.log(typeof(Number("123")), "转为数字类型")console.log(typeof(parseFloat("123")), "转为带小数点的数字类型") python: print(type( str(1) ),"转为字符串类型")print(type( int("123") ),"转为数字类型")print(type( float("123") ),"转为浮点类型")
刪除變數
JavaScript: var d = "aaa"console.log(d)delete d python: d="aaa"print(d)del d
2.字串
字串截取
JavaScript: //"xxx".substring(开始索引,结束索引但不包括 结束索引 处的字符)//"xxx".substring(开始索引,截取长度)var e = "0123456abcdef"console.log("完整截取:", e.substring(0, e.length));console.log("完整截取:", e.substr(0, e.length));console.log("截取012:", e.substring(0, 3));console.log("截取012:", e.substr(0, 3));console.log("截取索引为10值:", e[10]); python: e="0123456abcdef"print("完整截取:",e[:-1])print("截取012:",e[0:3])print("截取索引为10值:",e[10])
字串更新
JavaScript: console.log("更新字符串 :", e.substr(0, 6) + 'hahahhaha!') python: print("更新字符串 :", e[:6] + 'hahahhaha!')
英文大小寫轉換
JavaScript: console.log("转大写:", e.toUpperCase());console.log("转小写:", e.toLowerCase()); python: print("转大写:",e.upper())print("转小写:",e.lower())
判斷是否有字元
JavaScript: console.log("正确输出:", e.indexOf("a"))console.log("错误输出:", e.indexOf("A")) python: print("正确输出:","a" in e)print("错误输出:","A" in e)
輸出指定重複數
JavaScript: console.log("10输出:", new Array(10 + 1).join(e)) //通过将空数组拼接时中间插入字符串 python: print("10输出:",e*10)
換行輸出
JavaScript:
console.log("第一行\n" + "第二行\n" + "第三行\n");
python:
print('''第一行 第二行 第三行''');
切割字串
JavaScript: console.log(e.split(''))console.log(e.split('a')) python: print(list(e))print(e.split('a'))
4.循環遍歷
輸出0~99的整數
JavaScript: for (var o = 0; o < 100; o++) { console.log(o)} python: for i in range(0,100): print(i)
陣列遍歷
JavaScript:
var f = ["0", "1", "2", "3", "4", "5", "6", "a", "b", "c", "d", "e", "f"];for (i in f) { console.log("值:" + f[i], "索引" + i);}//或使用:f.forEach(function(v, i) { console.log("值:" + v, "索引" + i);}); python: #普通遍历f=["0", "1", "2", "3", "4", "5", "6", "a", "b", "c", "d", "e", "f"]for i in f: print(i)#含索引for i,v in enumerate(f): print("值:"+v,"索引"+str(i))#或含索引for i in range(0,len(f)): print("值:" + f[i], "索引" + i)
5.函數
JavaScript:
//空函数function g0() {}//带参数与返回值function g1(v1, v2, v3 = "默认值") { return 1;}//带参数与返回值或g2 = function(v1, v2, v3 = "默认值") { return 2;}//多返回值function g3(v1, v2, v3 = "默认值") { return [1, 2];} python: #空函数def g0():pass#带参数与返回值def g1(v1,v2,v3="默认值"): return 1#多返回值def g3(v1,v2,v3="默认值"): return 1,2
6.集合
JavaScript: arr=[1,2,3,1,1,1,"ccc","effd","ccc"]//去除重复//略.....//数组拼接var c=arr.concat([9,10,11] );//获取长度console.log(c.length);//判断是否存在console.log(c.indexOf(3));//追加元素c.push("元素"); python arr=[1,2,3,1,1,1,"ccc","effd","ccc"]#去除重复 print(list(set(arr)) )#数组拼接c=ar+[9,10,11] ;#获取长度print(len(c))#判断是否存在print(3 in c)#追加元素c.append("元素");
我相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
#相關閱讀:
################################## #######以上是python3與JS有什麼不同的詳細內容。更多資訊請關注PHP中文網其他相關文章!