Home >Backend Development >Python Tutorial >Why is there a big difference between python3 and 2?

Why is there a big difference between python3 and 2?

silencement
silencementOriginal
2019-06-24 15:26:182733browse

Why is there a big difference between python3 and 2?

python2: print statement, the statement means that you can directly follow the thing to be printed, and python3: print function, the function thinks that it must be called with parentheses. The following article will introduce to you the simple difference between python2 and python3. Interested friends can follow the editor to take a look.

python2: print statement, the statement means that you can directly follow the thing to be printed. If it is followed by is a tuple object, which can be printed directly

python3: print function, the function thinks that it must be called with parentheses. If it is connected to a tuple object, it can receive multiple positional parameters and can print

1. Expression

To get the calculation expression in Python 2, you would type:

X = raw_input ("enter some values)

But in Python 3, you would type:

X = input ("enter some values")

So, no matter what we enter, the value will be assigned to the variable x in 2 and 3. When typing 2*6 in Python 2, the result will be 12, which is the evaluated value.

However, when running the same program in Python 3, the result is a string value. In this case it looks like 2*6 in string format.

So, how do we get the evaluation expression? Now, we have to use an expression or function called eval. When you write eval before input, it converts the expression into a calculated value.

3. Range function

Range is used to generate a list of numbers, usually used to iterate in a for loop.

Here you can see that X equals Range 10. When we checked the variable X, it returned the list type. This means that in Python 2, Range is the type of a list. When I write X, I get a list of objects, here it is: 0 1 2 3 4 5 6 7 8 9.

Now let's move to Python 3. 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, Range is a range object itself.

The following is a brief summary of the differences between python2 and python3 in the following eight points

1. The code of python2 is confusing, repetitive and redundant because the people who wrote it at that time were experts in C language and Java. Daniel and other great masters, so there are shadows of various languages ​​in python3. After a summer vacation organized by Uncle Gui, the code was finally unified in November 2018. The source code specification is clear, simple and beautiful.

2. python3 print ("content"), python2 ptint() or print 'content'

3. python3 encoding: utf-8, python2 encoding: Default encoding: ascii Solution: In the first line # -*- encoding: utf-8-*-

4. User interaction input, python2: raw-input (), python3: input ()

5. python2x: Unicode defaults to 2 bytes to represent one character. You can make adjustments during LINUX compilation and installation. python3x: Unicode defaults to 4 bytes to represent one character.

6. python2x does not have nonlocal, python3x added

7. If you delete the init file in the newly created package of python3x, the package can still be called. If there is no init file in the newly created package of python2x, the package cannot be called and an error will be reported directly.

8. Classics in python2 The class traversal method is depth-first, and the new-style classes are breadth-first. There are no classic classes in python3. All classes are new-style classes, so they are breadth-first.

The above is the detailed content of Why is there a big difference between python3 and 2?. 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