What 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('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({}))
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) + 'hahahhaha!') python: print("更新字符串 :", e[:6] + 'hahahhaha!')
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('''第一行 第二行 第三行''');
Cut string
JavaScript: console.log(e.split(''))console.log(e.split('a')) python: print(list(e))print(e.split('a'))
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

What graphics card is good for Core i73770? RTX3070 is a very powerful graphics card with excellent performance and advanced technology. Whether you're playing games, rendering graphics, or performing machine learning, the RTX3070 can handle it with ease. It uses NVIDIA's Ampere architecture, has 5888 CUDA cores and 8GB of GDDR6 memory, which can provide a smooth gaming experience and high-quality graphics effects. RTX3070 also supports ray tracing technology, which can present realistic light and shadow effects. All in all, the RTX3070 is a powerful and advanced graphics card suitable for those who pursue high performance and high quality. RTX3070 is an NVIDIA series graphics card. Powered by 2nd generation NVID

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Which tablet is suitable for musicians? The 12.9-inch speaker in Huawei’s iPad is a very good product. It comes with four speakers and the sound is excellent. Moreover, it belongs to the pro series, which is slightly better than other styles. Overall, ipad pro is a very good product. The speaker of this mini4 mobile phone is small and the effect is average. It cannot be used to play music externally, you still need to rely on headphones to enjoy music. Headphones with good sound quality will have a slightly better effect, but cheap headphones worth thirty or forty yuan cannot meet the requirements. What tablet should I use for electronic piano music? If you want to buy an iPad larger than 10 inches, I recommend using two applications, namely Henle and Piascore. Provided by Henle

What driver is good to install on rx5808g? 20.5.1 and 20.4.2WHQL refer to the version number of the software or driver. These version numbers are typically used to identify updates or fixes to software or drivers. In the computer world, WHQL stands for Windows Hardware Quality Labs, which is an institution used by Microsoft to test and verify the compliance and stability of hardware and drivers. Therefore, 20.5.1 and 20.4.2WHQL indicate that these software or drivers have passed Microsoft's testing and verification and can be safely used in the Windows operating system. AMDrx580 graphics card relatively stable drivers 20.5.1 and 20.4.2WHQL refers to the version number of the software or driver. These version numbers are passed

What games can be played with i34150 with 1G independent graphics? Can it play small games such as LoL? GTX750 and GTX750TI are very suitable graphics card choices. If you just play some small games or not play games, it is recommended to use the i34150 integrated graphics card. Generally speaking, the price difference between graphics cards and processors is not very big, so it is important to choose a reasonable combination. If you need 2G of video memory, it is recommended to choose GTX750TI; if you only need 1G of video memory, just choose GTX750. GTX750TI can be seen as an enhanced version of GTX750, with overclocking capabilities. Which graphics card can be paired with i34150 depends on your needs. If you plan to play stand-alone games, it is recommended that you consider changing the graphics card. you can choose
