Python3 or Python2? Examples explain the differences between the two
mob604756fb8908 2021-07-20 04:34:54

original text :Key differences between Python 2 and 3: How to navigate change

compile : Open source in China (oschina2013)

translator :OSC- Darwin

If you need to reprint, please indicate the above information in the text

After each programming language releases an update , Big changes happen between major versions . In this paper ,Vinodh Kumar This is explained by an example Python 2 and Python 3 Some of the big differences between , To help explain the changes in language .

This tutorial is mainly about :


expression


Print Options


Unequal operation


Range


Automatic migration


Performance issues


Major internal transaction changes

1、 expression

stay Python 2 In order to get the evaluation expression , You can type it :

But in Python 3 in , You can type it :

therefore , Whatever we type in , Values are assigned to 2 and 3 The variables in the x. When in Python 2 Input in 2*6 when , The result will be 12, This is the evaluation value .

however , When in Python 3 When running the same program in , The result is a string value . under these circumstances , It looks like a string format 2*6.

that , How do we get the evaluation expression ? Now? , We have to use one called eval An expression or function of . When you write before typing eval when , It converts the expression to a calculated value .

Specific expression examples :

stay Python 2 in :

Then the output :

stay Python 3 in :

Then the output :

We can see clearly that , The difference between them is very small .

2、Print Options

stay Python 2 in ,print It's a statement that doesn't need parentheses . stay Python 3 in ,print It's a function , Values need to be enclosed in brackets .

Python 2

Input :

Will be output :

Python 3

Input :

Will be output :

3、Unequal operation

When we're in Python 2 Use in Unequal Operator , We need to use more than > Or less than < Symbol . however , stay Python 3 in , There is a general operator . Exclamatory mark ! And the equal sign = Used to indicate whether values are equal .

Python 2 -<> The operator represents inequality
Python 3 -! The operator represents inequality

Python 2

Input :

Will be output :

Python 3

Input :

Will be output :

4、Range

Range Used to generate a list of numbers , Usually used for iterations for loop .

Here you can see X be equal to Range 10. When we look at variables X when , It returns the list type . This means that Python 2 in ,Range Is the type of list . When I write X after , Get a list of objects , Here is :0 1 2 3 4 5 6 7 8 9.

Now let's turn to Python 3, When we write X be equal to Range 5, This value is assigned to the variable X; When we look at variables X The type of , It will return one Range Object itself . This means that Python 3 in ,Range It's a scope object itself .

Python 2

Input :

Will be output :

Python 3

Input :

Will be output :

5、 Automatic migration

that , How do we automate scripts to take code from Python 2 Move to 3?

ad locum , We can use Python Medium Add 2 Numbers To test .

**
Python 2**

Input :

Will be output :

So here we see that it can be passed on the command line 2to3 Convert to Python 3 Code .

Python Provided their own tools , be known as 2to3.py, It runs a bunch of scripts to put your Python 2 Code conversion to 3. Although it's not perfect , But on the whole it's done very well . After converting any code , You can fix any problem manually .

6、 Performance issues

Most performance issues have been fixed in this upgrade ! When comparing benchmarks between two versions , The difference is almost negligible .

**
7、 Some major internal changes **

Python 2


print Function brackets are optional .


Use u As a prefix string to generate unicode character string .


Integer division always returns an integer -5/2=2.


Raw_input() Read string .


input() Evaluate the data read .


generator .next().

Python 3:


print Function brackets are required .


The default is string unicode.


Integer division can lead to floating -5/2=2.5.


Raw_input() Unavailable .


Input always reads string .


Next (generator).


Py2 to py3 Utilities .


Dictionary .keys() and .values() The view returned is not a list .


You can no longer use comparison operators in unnatural comparisons .
for example ,None < None Will lead to TypeError Instead of going back false.


Percentage is not recommended (%) String formatting operators , Use .format() Function or connection .

Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09