这次给大家带来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中文网其他相关文章!