Home >Web Front-end >Front-end Q&A >What is the difference between python and javascript

What is the difference between python and javascript

青灯夜游
青灯夜游Original
2021-11-03 16:00:434270browse

Difference: 1. Comments in JS use "//" and "/* */" characters, while Python uses "#" and """" """" characters; 2. Every comment in JS Each code block must be placed in curly braces, and Python does not use curly braces or parentheses; 3. Python does not support the " " and "--" operators, but JS does.

What is the difference between python and javascript

The operating environment of this tutorial: windows7 system, javascript1.8.5&&Python3 version, Dell G3 computer.

JavaScript and Python are two very important languages. Although many people, including me, know Python or JavaScript, we don’t know the important differences between the two languages.

I noticed that there are many semantic differences between these two languages. I believe that understanding these differences will be of great help to me and others. In this article, I will compare some basic concepts that I encountered in both languages. So, if you are familiar with JavaScript or Python and want to understand the differences between the two, you have come to the right place!

1. Comment differences

JavaScript

Use double slashes (/ /) as a single-line comment, /* */ as a multi-line comment.

Python

Use # as a single line comment, use triple quotes """ """ as a multi-line comment.

2. Differences in code blocks

JavaScript

Every code block in JavaScript Must be placed in curly braces ({}), and each statement must be ended with a semicolon ;.

var a = 2;
if(a>0){
   console.log("Positive");
}
else{
   console.log("Negative");
}

Python

Code blocks in Python are represented by indentation. It doesn't use curly braces or parentheses, it uses whitespace. Each statement requires a newline.

a = 2
if a>0:
   print("Positive")
else:
   print("negative")
   print("does not belong to else block")

3. Data type differences

I cannot fully discuss the data type differences between JavaScript and Python in this article. Here we only discuss the main differences. What is the difference between python and javascript

Basic data types

  • The above table shows the basic data types of JavaScript and Python. The Number type in JavaScript represents Int and float values, which are determined by the compiler at runtime. While BigInt in JavaScript (ES10) is used to store large integers, Python uses Int to represent integers of all sizes.

  • JS uses the null keyword to represent empty values, and Python uses None.

  • Python does not have the concepts of undefined and symbol, these are unique to JavaScript.

  • Python has a special data type - complex, used to represent complex numbers x yj, where x is the real part and y is the imaginary part.

a=3+4j
print(a.real) #3
print(a.imag) #4

What is the difference between python and javascript

Non-basic data types

  • The above figure shows the complex (or called non-basic) type of data. Lists in Python can store any data type, just like JavaScript.

  • But arrays in Python can be defined using libraries (such as NumPy, array). Arrays in Python can only contain uniform data types. Lists and arrays in Python are very different. There are many mathematical operations that can be performed on arrays that cannot be performed on lists.

  • Objects in JavaScript are similar to dictionaries in Python. Both contain key-value pairs. But objects are the basic building blocks in JavaScript, and dictionaries in Python are just data containers.

  • A tuple in Python is an unmodifiable list. Lists represented as tuples cannot be redefined. JavaScript does not have this concept.

  • So, Python has a built-in hash table (dictionary), while JavaScript has no built-in hash table, method or library.

4. Differences in variable definitions

JavaScript

To define variables in JavaScript, you need to use three The main keywords: var, let and const. The definition method determines how the variable is used (and its scope).

Python

You don’t need to use keywords to define variables in Python, you just need to assign a value directly to the variable name:

a = 3
print (a) # 3

5. If-Else difference

JavaScript

The conditional statements in JavaScript are if, else if, else and switch.

Python

The conditional statements in Python are if, elif and else.

elif is the abbreviation of else-if. Python does not have a switch statement. Instead, use a dictionary instead.

6. Differences between ternary operators

JavaScript

JavaScript ternary operator (?:) is a conditional operator, the syntax is (condition)?(expresssionIfTrue):(expressionIfFalse):

var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"

Python

The syntax of the ternary operator in Python is (expressionIfTrue) if (condition) else (expressionIfFalse)

a, b = 10, 20
min = a if a < b else b
print(min)

7. The difference between congruent operators

JavaScript

全等运算符(===)也叫“严格相等运算符”,会比较两个运算数并检查其相等性,而不会进行类型转换,也就是说,它会检查运算数的数据类型。返回值为布尔。JavaScript只有一个全等运算符。

var a=3,b="3";
console.log(a==b); // true
console.log(a===b); // false

Python

Python有两个全等运算符:is 和 is not。

is 运算符会测试两个运算数是否为同一个对象,is not为is的反面。

x = 5
if (type(x) is int):
print ("true") # true
if ( type(x) is not int):
print ("true")

8、成员运算符区别

JavaScript

成员运算符检查对象中的特定属性。JavaScript只有一个成员运算符:in。

const user= {name: 'Sara', age: 19, sex: "female"};
console.log('name' in car);
// output: true

Python

成员运算符用于验证某个值是否为成员。Python有两个成员运算符:in和not in。

x = 24
y = 20
list = [10, 20, 30, 40, 50 ]
if ( x not in list ):
print ("not present") # Output:True

9、循环区别

JavaScript

JavaScript有三种循环:

  • 入口控制循环:for和while(在执行循环语句之前测试条件)

  • 出口控制循环:do-while(执行循环语句之后测试条件)

// for loop
var c = ["red", "green", "blue", "purple"];
for (var i = 0; i<4; i++){
console.log(c[i]);
}
// prints array// while
var c = ["red", "green", "blue", "purple"];
while(i<4){
console.log(c[i]);
i++;
} // prints array

Python

Python有两种循环:for和while。Python没有内置的do-while循环。

Python的while循环与JavaScript的for循环类似。例如下面的例子:

c = ["red", "green", "blue", "purple"]
i = 0
while i<4:
print(c[i])
i += 1 # i++ is invalid

Python不支持++和--运算符。我们需要使用x+=1和x-=1。

for循环用来遍历序列(列表、数组和元组)。它使用in操作符和range()函数进行迭代。可以用range()生成一系列数字,该函数接受三个参数:start,stop和step。

  • start:开始的位置(可选)。默认为0。

  • stop:结束的位置(必须指定)。

  • step:指定序列中两个数字之间的间隔(可选)。

x = range(5)
for n in x:
print(n)           # 1,2,3,4x = range(3,9)
for n in x:
print(n)           # 3,4,5,6,7,8x = range(2,20,2)
for n in x:
print(n)           # 2,4,6,8,10,12,14,16,18

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What is the difference between python and javascript. 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
Previous article:What is es in javascriptNext article:What is es in javascript