Example to explain the main differences between Python2 and Python3
Every programming language changes significantly between major versions after the release of an update. In this article, Vinodh Kumar explains some of the major differences between Python 2 and Python 3 with examples to help illustrate the changes in the language.
This tutorial focuses on the following.
(located) at Python 2 in order to obtain the calculation expression (math.), You would type:
X=raw_input("entersomevalues)
But in Python 3, you would type.
X=input("entersomevalues")
Thus, whatever we enter, the values are assigned to the variables x in 2 and 3. When you type 2*6 in Python 2, the result will be 12, which is the evaluation value.
However, when the same program is run in Python 3, the result is a string value. In this case, it looks like 2*6 in string format.
or so, How we get assessments expression (math.) this (Cantonese)? present ., We have to use a file called eval of expression (math.) or function。 When you write before you enter eval time, It will bring expression (math.) Convert to calculated value。
x=eval(input("entersomevalues"))=12
Examples of specific expressions.
In Python 2.
name=input("Whatisyourname?")print("Hello,%s."%name)
Then output.
In Python 3.
name=input("Whatisyourname?")print("Hello,%s."%name)
Then output.
We can clearly see that there is very little difference between them.
In Python 2, print is a statement that does not require parentheses. In Python 3, print is a function and the value needs to be enclosed in parentheses.
Python 2
Input.
print"helloworld"
Will output.
Python 3
Input.
1!=1.0print(False)
Will output.
When we are in the Python 2 use in Unequal operator time, We need to use a value greater than > or less than < symbolic。 nevertheless,(located) at Python 3 in, There is a generic operator。 exclamation mark ! (punct.) ! and equal sign = Used to indicate whether values are equal。
Python 2 -<> Operators indicate inequality Python 3 -! Operators indicate inequality
Python 2
Input.
1<>1.0print"False"
Will output.
Python 3
Input.
1!=1.0print(False)1!=1.0print(False)
Range is used to generate a list of numbers, usually for iterating through a for loop.
Here you can see that X is equal to Range 10. When we check the variable X, it returns the list type. This means that in Python 2, Range is the type of a list. After I write X, I get a list of objects, here: 0 1 2 3 4 5 6 7 8 9.
Now let's move to Python 3, where when we write X equals Range 5, this value is assigned to the variable X; when we check the type of the variable X, it returns a Range object itself. This means that in Python 3, a Range is a range object itself.
Python 2
Input.
printrange(0,10,1)
Will output.
Python 3
Input.
print(list(range(10)))
Will output.
So, how do we automate the execution of scripts to move code from Python 2 to 3?
Here, we can use Add 2 Numbers in Python for testing.
Python 2
Input.
n1=1n2=2add=float(n1)+float(n2)print'sumof{0}and{1}is{2}'.format(n1,n2,add)
Will output.
Now using the 2to3 migration, we can convert the code above.
Input.
n1=1n2=2add=float(n1)+float(n2)print('sumof{0}and{1}is{2}'.format(n1,n2,add))
Will output.
So here we see that it can be converted to Python 3 code on the command line via 2to3.
Python provides its own tool called 2to3.py, which runs a bunch of scripts to convert your Python 2 code to 3. It's not perfect, but it does a great job overall. After converting any code, you can fix any issues manually.
Most performance issues have been fixed in this update! When comparing the benchmarks between the two versions, the differences are almost negligible.
Python 2
Python 3:
Compiled from: Key differences between Python 2 and 3: How to navigate change