Home > Web Front-end > JS Tutorial > body text

What is the difference between python3 and JS

php中世界最好的语言
Release: 2018-03-10 16:24:02
Original
1628 people have browsed it

This time I will bring you the difference between python3 and JS, and what are the precautions for using python3 and JS. The following is a practical case, let's take a look.

0. Comments and code blocks

JavaScript:
//单行注释/* * 多行 * 注释 */
python:
#单行注释'''多行注释'''
Copy after login

Line and indentation

The biggest difference between learning Python and other languages ​​​​is that Python code blocks do not use curly brackets ({}) to control classes, functions and other logical judgments. The most distinctive feature of Python is the use of indentation to write modules.

1. Variables

Variable declaration and assignment

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);
Copy after login

Variable exchange

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);
Copy after login

Common variable types

JavaScript:
//typeof(??)<--用来查看类型console.log(typeof(1))console.log(typeof(1.0))console.log(typeof(&#39;a&#39;))console.log(typeof(&#39;aaaa&#39;))console.log(typeof([]))console.log(typeof({}))
python:
#type(??)<--用来查看类型print(type(1))print(type(1.0))print(type(&#39;a&#39;))print(type(&#39;aaaa&#39;))print(type([]))print(type({}))
Copy after login

Common variable type conversion

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") ),"转为浮点类型")
Copy after login

Delete variables

JavaScript:
var d = "aaa"console.log(d)delete d
python:
d="aaa"print(d)del d
Copy after login

2. String

String interception

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])
Copy after login

String update

JavaScript:
console.log("更新字符串 :", e.substr(0, 6) + &#39;hahahhaha!&#39;)
python:
print("更新字符串 :", e[:6] + &#39;hahahhaha!&#39;)
Copy after login

English case conversion

JavaScript:
console.log("转大写:", e.toUpperCase());console.log("转小写:", e.toLowerCase());
python:
print("转大写:",e.upper())print("转小写:",e.lower())
Copy after login

Determine whether characters exist

JavaScript:
console.log("正确输出:", e.indexOf("a"))console.log("错误输出:", e.indexOf("A"))
python:
print("正确输出:","a" in e)print("错误输出:","A" in e)
Copy after login

Output the specified number of repetitions

JavaScript:
console.log("10输出:", new Array(10 + 1).join(e)) //通过将空数组拼接时中间插入字符串
python:
print("10输出:",e*10)
Copy after login

Newline output

JavaScript:

console.log("第一行\n" + 
"第二行\n" + "第三行\n");
Copy after login

python:

print(&#39;&#39;&#39;第一行
第二行
第三行&#39;&#39;&#39;);
Copy after login

Cut string

JavaScript:
console.log(e.split(&#39;&#39;))console.log(e.split(&#39;a&#39;))
python:
print(list(e))print(e.split(&#39;a&#39;))
Copy after login

4. Loop traversal

Output integers from 0 to 99

JavaScript:
for (var o = 0; o < 100; o++) {    console.log(o)}
python:
for i in range(0,100):    print(i)
Copy after login

Array traversal

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)
Copy after login

5. Function

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
Copy after login

6. Collection

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("元素");
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese Other related articles online!

Related reading:

How to use s-xlsx to import and export Excel files

How to use JavaScript Save text data

Browser file segmentation breakpoint upload

File upload extension made with jQuery

The above is the detailed content of What is the difference between python3 and JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template