Python 2 vs Python 3: Which to Learn?
If you’re new to Python perhaps you’ve heard there are two major versions out in the wild: Python 2 and Python 3. So which should you learn and why is there a debate about this?
The short answer is that if you’re a beginner, you should focus on Python 3. And if you’re an experienced programmer, unless you are inheriting a legacy software project, you should also use Python 3 for your future software projects.
Why are there two versions of Python?
A very brief history of Python helps explain why there are two active versions today. Python was first released in 1991 by Guido van Rossum and has been in continual development since then. Python 2.0 was released in 2000 followed by Python 3.0 in 2008, each bringing major new updates to the language.
Python 3 is considered the future of Python and was developed to address intrinsic design flaws in previous versions of the language. The focus is on cleaning up the underlying codebase and removing redundancy so that it is clear the one way to perform a given task.
Python 3 was slowly adopted because it was backwards-incompatible, meaning most Python 2 code would not run on unmodified Python 3. This split resulted in many package libraries not adding full Python 3 support for quite some time.
During this period, several prominent Pythonistas–most notably Armin Ronacher, the author of the Flask framework, and Zed Shaw, author of Learn Python the Hard Way–publicly argued against upgrading to Python 3. As of late 2016, Zed Shaw still maintains that beginners should avoid Python 3.
While Python 2 and 3 are quite similar, there are notable differences in code handling and syntax. You should not assume that code written in Python 2 will work smoothly in Python 3 or vice versa.
In Python 2,
print "I'm a print statement in Python 2"
In Python 3,
print() is explicitly treated as a function, so we use standard function syntax:
print("I'm a print statement in Python 3")
Note that Python 3’s
print() syntax is backwards-compatible with Python 2.7, so a Python 3
print() function will run in either version.
In Python 2, numbers typed without decimals were treated as integers and the default behavior of the
/ symbol was floor division. In practice that meant that expressions evaluated to non-intuitive results:
# Python 2 x = 5 / 2 print x # Output is 2
To override this behavior, you could manually add decimal places
5.0 / 2.5, explicitly treating the numbers as floats rather than integers to achieve the expected result of
In Python 3, integer division is more intuitive.
# Python 3 x = 5 / 2 print(x) # Output is 2.5
You can still use floats like
5.0 / 2.5 to return
2.5, but if you want to use floor division you use the Python 3 syntax of
# Python 3 y = 5 // 2 print(y) # Output is 2.5
Note that this feature of Python 3 is not backwards-compatible with Python 2.
There are several ways for a programming language to treat the
string type. Python 2 uses the ASCII alphabet, so when you type a string like
"Hello, World" it is handled as ACII which is unfortunately limited to several hundred characters and is not very flexible for encoding non-English characters. To use Unicode character encoding, which supports 128,000+ characters across modern and historic languages, you’d have to type
u"Hello, World", with the
u prefix donating Unicode.
In Python 3, Unicode is used by default. This allows for far greater language support as well as displaying items like emojis.
Python 3 in 2017
So where are today? As of 2017 it’s been 9 years since Python 3 was first introduced. Since that time:
- most major software packages now work on Python 3
- Django recommends using Python 3 and new versions no longer support Python 2
- development on Python 2.7 itself will stop after 2020
- Python 3 continues to make notable performance and security improvements, including the addition of asynchronous support
While there are several notable differences, migrating an existing project from Python 2 to Python 3 is quite manageable on legacy projects. But if you are learning Python or starting a new project, you should use Python 3 to take advantage of continued advancements as well as better ongoing support within the broader Python community.
Note: Need help installing Python 3? Learn how to install Python 3 on your Mac computer.