1. Differences Between Python 2 and 3
| Feature \ Version | Python 2 | Python 3 |
|---|---|---|
| print as a function | print"abc" | print(“abc”) |
| Unified class | Old-style and new-style classes | New-style classes only |
| Floating-point division | 1/2=0 | 1/2=0.5 |
| String formatting | %, Format | Format,% |
| xrange replaces range | xrange | range |
| long renamed to int | Long,int | Int |
| Package imports | Relative imports | Absolute imports |
| Source file encoding | Ascii | utf8 |
The official Python recommendation is strongly in favor of learning Python 3 directly, since Python 2 is only maintained through 2020. But at this stage (2018), a large amount of production is still Python 2, so learning Python 2 first is advisable. At the same time, the differences between Python 2.7 and Python 3 are no more than 10%, and the __future__ library in Python 2 contains a large number of Python 3 features.
In practice, the recommendations are:
- Become familiar with the
__future__library - Use syntax compatible with both Python 2 and Python 3 as much as possible
- Understand the syntax and packages deprecated in Python 3,
- Do not use Python 3.x versions earlier than Python 3.5
