Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between python and javascript

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what are the differences between python and javascript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the differences between python and javascript"?

Differences: 1, JS comments with "/ /" and "/ * /" characters, while Python with "#" and "; 2, JS each code block must be placed in curly braces, and Python does not use curly braces or parentheses; 3, Python does not support" + "and"-"operators, while JS supports.

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 difference between the two languages.

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

1. Differences in annotations

JavaScript

Use double slashes (/ /) as single-line comments and / * / as multiline comments.

Python

Use # as a single-line comment and three quotes as a multiline comment.

2. Code block difference

JavaScript

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

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

Python

Blocks of code in Python are represented by indentation. Instead of using curly braces or parentheses, it uses white space. Each statement needs to be wrapped.

A = 2if a > 0: print ("Positive") else: print ("negative") print ("does not belong to else block") 3, data type difference

I can't fully discuss the data type differences between JavaScript and Python in this article; we'll only discuss the main differences here.

Basic data type

The table above 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 run time. BigInt in JavaScript (ES10) is used to store large integers, while Python uses Int to represent integers of all sizes.

JS uses the null keyword to denote null values, and Python uses None.

Python has no concepts of undefined and symbol, which are unique to JavaScript.

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

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

Non-basic data type

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

However, arrays in Python can be defined using libraries such as NumPy,array. Arrays in Python can only contain uniform data types. Lists in Python are very different from arrays. Many mathematical operations can be performed on an array, but not on a list.

Objects in JavaScript are similar to dictionaries in Python. Both contain key-value pairs. But objects are a basic part of JavaScript, while dictionaries are just data containers in Python.

Tuples in Python are an immutable list. Lists in tuples cannot be redefined. JavaScript does not have this concept.

Therefore, Python has a built-in hash table (dictionary), while JavaScript does not have a built-in hash table, method, or library.

4. The difference of variable definition

JavaScript

Defining variables in JavaScript requires the use of three main keywords: var,let and const. The definition method determines how the variable is used (and scope).

Python

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

A = 3print (a) # 35, 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, you can use a dictionary instead.

6. Differences between ternary operators

JavaScript

The ternary operator (?:) of JavaScript is a conditional operator and the syntax is (condition)? (expresssionIfTrue): (expressionIfFalse):

Var age = 26 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, 20min = an if a < b else bprint (min) 7, congruent operator difference

JavaScript

The congruence operator (= =), also known as the strict equality operator, compares two operands and checks for equality without type conversion, that is, it checks the data type of the Operand. The return value is Boolean. JavaScript has only one congruent operator.

Var axiom 3 ~ (?) b = "3"; console.log (averse roomb); / / trueconsole.log (axiomorphic roomb); / / false

Python

Python has two congruent operators: is and is not.

The is operator tests whether two operands are the same object, and is not is the opposite of is.

X = 5if (type (x) is int): print ("true") # trueif (type (x) is not int): print ("true") 8, member operator distinction

JavaScript

The member operator checks for specific properties in the object. JavaScript has only one member operator: in.

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

Python

The member operator is used to verify that a value is a member. Python has two member operators: in and not in.

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

JavaScript

JavaScript has three loops:

Entry control loop: for and while (test the condition before executing the loop statement)

Exit control loop: do-while (test condition after loop statement is executed)

/ / for loopvar c = ["red", "green", "blue", "purple"]; for (var I = 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report