Many beginning Python users are wondering with which version of Python they should start. My answer to this question is usually something along the lines "just go with the version your favorite tutorial was written in, and check out the differences later on."
But what if you are starting a new project and have the choice to pick? I would say there is currently no "right" or "wrong" as long as both Python 2.7.x and Python 3.x support the libraries that you are planning to use. However, it is worthwhile to have a look at the major differences between those two most popular versions of Python to avoid common pitfalls when writing the code for either one of them, or if you are planning to port your project.
__future__
module¶Python 3.x introduced some Python 2-incompatible keywords and features that can be imported via the in-built __future__
module in Python 2. It is recommended to use __future__
imports it if you are planning Python 3.x support for your code. For example, if we want Python 3.x's integer division behavior in Python 2, we can import it via
from __future__ import division
More features that can be imported from the __future__
module are listed in the table below:
feature | optional in | mandatory in | effect |
---|---|---|---|
nested_scopes | 2.1.0b1 | 2.2 | PEP 227: Statically Nested Scopes | </tr>
generators | 2.2.0a1 | 2.3 | PEP 255: Simple Generators | </tr>
division | 2.2.0a2 | 3.0 | PEP 238: Changing the Division Operator | </tr>
absolute_import | 2.5.0a1 | 3.0 | PEP 328: Imports: Multi-Line and Absolute/Relative | </tr>
with_statement | 2.5.0a1 | 2.6 | PEP 343: The “with” Statement | </tr>
print_function | 2.6.0a2 | 3.0 | PEP 3105: Make print a function | </tr>
unicode_literals | 2.6.0a2 | 3.0 | PEP 3112: Bytes literals in Python 3000 | </tr> </table>