Python is a popular programming language for beginners. It’s both powerful and extremely versatile, used in just about every industry and for many different types of coding projects. So, if you’re just getting into programming, Python is a great choice as it’s both easy to learn and in demand.
Python was first released in 1991 by the Dutch programmer Guido van Rossum. Since then, it’s gone through many changes. Today, there are three major versions of Python. Of those, only the last two versions — Python 2 and Python 3 — are still in use.
This can be confusing to new developers. Which should you learn, Python 2 or Python 3? Generally, the answer is the latter, but there are many cases in which it helps to know the former.
To help you decide which version of Python is right for you, we’ll explore the language’s history and development, the differences between Python 2 and Python 3, and the use cases for each in the paragraphs below.
History of Python 2 vs. Python 3
Python 2 came out in 2000. The upgrade to the language was designed to make it easier for the average person to learn, but it also added many features developers needed, like list comprehension, Unicode support, garbage collection, and improved support for object-oriented programming. With all these benefits, it wasn’t a hard choice for developers to upgrade their Python 1 code to Python 2.
Python 3 came out in 2008 after van Rossum found new ways to improve the language. You learn as you go in programming, and sometimes problems only show up late in the process. But these improvements could only be made with a new version of Python that wouldn’t be backward compatible with Python 2.
Python 3’s lack of backward compatibility was an issue for many developers, as they relied on the third-party libraries written to support Python 2 to expedite the development of their applications. Given the popularity of Python and its associated libraries, Python 2 code was everywhere, and it was hard to get people to make the change.
The last version of Python 2 was released in 2010, after the initial release of Python 3. Initially, the support for Python 2 was supposed to end in 2015, but because it was still so widely used, the deadline got pushed back until January 2020.
What’s the difference between Python 2 and Python 3?
Python 3 brought many changes to the language, both large and small. For example, with Python 2, division between integers always returned another integer — so if you divided 7 by 5, you’d get 1. With Python 3, the same formula will return the actual value of 1.4 as a float.
Another change involved the print keyword in Python 2 becoming the print() function in Python 3, as seen in the examples below.
print "Hello World"
A bigger change between the two versions of the language laid in their default character encoding of strings. Character encoding is how a programming language stores characters in bytes. Python 2’s default character encoding, ASCII, only allowed for the use of letters, digits, and punctuation marks.
Python 3, on the other hand, uses Unicode, which supports the English, Arabic, and Greek alphabets (among others), along with mathematical symbols, emojis, and more. Before the release of Python 3, most modern programming languages had already made the switch to Unicode.
Python 2 is an untyped language. This means you can create a variable as a string, like a name, and later assign it to an integer, like 2. Python 3 supports typing but doesn’t enforce it. This means that developers can still write Python 3 code in the same style they wrote Python 2 code, but they can also choose to use type hints to write code that is more understandable, useable, and efficient.
Why should you learn Python 3?
Python 3 is the obvious choice for any developer that is beginning to learn to program. The changes that were made to the language make it easier to write quality code using the language. This is why the version was created in the first place. It just took a while for the Python community to catch up and upgrade all the third-party libraries that developers depended on to get the job done.
It took quite a few years for most of these third-party libraries to be upgraded to Python 3. But now, you’ll find a larger selection of libraries supporting Python 3 than the older version. And, as of the beginning of 2020, Python 2 is no longer being developed. The last version of Python 2 had been in maintenance mode for years by then, and all its new features were added to Python 3.
For the most part, you won’t find much Python 2 code in use today.
Why should you learn Python 2?
While you may not find much Python 2 code in use, you may still run into it from time to time. Most companies have what they call legacy code. This is code that is already working, and development on it has finished except for fixing bugs that may pop up once in a while. You might see Python 2 code in companies that don’t see the upgrade to Python 3 as worth the effort.
You may also run into the rare Python 2 library, and if it’s necessary for your project and there are no alternatives, you may have no other choice than to write Python 2 code. Some of the more popular libraries that still support Python 2 include Fabric and Ansible, which are tools used in DevOps to manage the configuration of servers.
Getting started with Python
Python has a long history now but has only gone through three major changes in its 30 years. For the longest time, most developers chose Python 2 over Python 3 because most of the Python ecosystem was written in the former, even though the latter was a big improvement. But that’s changed.
Today, most libraries are written in Python 3 because Python 2 is no longer being maintained. You may still run into Python 2 code once in a while, but the actual differences between the languages are easy to learn. If you know Python 3, you can write Python 2 code if necessary. To get started learning Python 3 today, check out our Learn Python 3 course.