search
HomeWeb Front-endJS TutorialWhat is the difference between python3 and JS

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:
#单行注释'''多行注释'''

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);

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);

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({}))

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

Delete variables

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

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])

String update

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

English case conversion

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

Determine whether characters exist

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

Output the specified number of repetitions

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

Newline output

JavaScript:

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

python:

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

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;))

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)

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)

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

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

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!

Statement
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
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.