This page looks best with JavaScript enabled

Python2 VS Python3

 ·  ☕ 1 min read

1. Differences Between Python 2 and 3

Feature \ VersionPython 2Python 3
print as a functionprint"abc"print(“abc”)
Unified classOld-style and new-style classesNew-style classes only
Floating-point division1/2=01/2=0.5
String formatting%, FormatFormat,%
xrange replaces rangexrangerange
long renamed to intLong,intInt
Package importsRelative importsAbsolute imports
Source file encodingAsciiutf8

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

2. References


WeChat Official Account
WRITTEN BY
WeChat Official Account