Many Python beginners ask: Which version of Python should I learn? My answer to this question is usually "choose a python tutorial that works best for you, which version of Python is used in the tutorial, and you use that version." And so on, and then to study the differences between the different versions.
Many Python beginners ask: Which version of Python should I learn? My answer to this question is usually "choose a python tutorial that works best for you, which version of Python is used in the tutorial, and you use that version." And so on, and then to study the differences between the different versions.
But if you want to develop a new project in Python, how do you choose the Python version? I can responsibly say that most Python libraries support both the Python 2.7.x and 3.x versions, so whichever version you choose is OK. But in order to avoid some of the common pitfalls of some versions while using Python, or to migrate a Python project, it is still necessary to understand the main differences between the two common Python versions.
__future__ Module
Python 3.x introduces some of the keywords and features that are incompatible with Python 2, and in Python 2, you can import these new content through the built-in __future__ module. If you want code written in the Python 2 environment to run in Python 3.x, it is recommended that you use the __future__ module. For example, if you want to have the Python 3.x integer division behavior in Python 2, you can import the corresponding module through the following statement.
From __future__ Import Division
The following table lists the other features that can be imported in __future__:
2.2.0a2
attributes |
Optional version |
enforce version |
effect |
nested_scopes |
2.1.0b1 |
2.2 |
PEP 227:statically Nested scopes |
generators |
2.2.0a1 |
2.3 |
PEP 255:simple generators |
Division |
3.0 |
PEP 238:changing the division Operator |
Absolute_impor T |
2.5.0a1 |
3.0 |
PEP 328:imports:multi-line and absolute/relative |
with_statement |
2.5.0a1 |
2.6 |
PEP 343:the "with" Statement |
print_function |
2.6.0a2 |
3.0 |
PEP 3105:make print a function |
unicode_literals |
2.6.0a2 |
3.0 |
PEP 3112:bytes literals in Python 3000 |
(Source: https://docs.python.org/2/library/future.html)
Example:
From platform import python_version
Print function
Although the print syntax is a small change in Python 3 and should already be well known, it is worth mentioning that the print statement in Python 2 is replaced by the print () function in Python 3, which means that the Python You must enclose the object you want to output in parentheses in 3.
It is also possible to use extra parentheses in Python 2. But conversely, when you want to call the print function without parentheses in the form of Python2 in Python 3, the syntaxerror is triggered.
Python 2
print ' Python ', python_version ()
print ' Hello, world! '
Print (' Hello, world! ')
Print "text",; print ' Print more text on the same line '
Python 2.7.6
Hello, world!
Hello, world!.
text Print more text on the same line
Python 3
Print (' Python ', Python_version ())
print (' Hello, world! '
) Print ("Some text,", end= "")
print (' Print more text on the same line ')
Python 3.4.1
Hello, world!
Some text, print more text on the same line
print ' Hello, world! '