Python is an excellent and very popular programming language, considered one of the best-designed. It has a simple, clear syntax and runs very fast (faster if you compile it with CPython), but there is one slight problem facing anyone new to it: Which version should you learn?
There are two versions of Python—Python 2 and Python 3. Python 2.7 is the last of the 2.* series (currently up to 2.7.7) and has been the recommended version to learn for ages. Python 3 has been seen as not-quite-ready, though worth trying if you wanted. The current version is 3.4.1.
What’s the Problem With Python 3?
Python 3 was introduced in December 2008 to correct deficiencies perceived by its inventor, Guido Van Rossum. This included changing core strings to be fully Unicode-compatible, changing print to be a function, division with / and returning a list of ints from the range() function. Add to this exception-chaining, function annotations, extended tuple unpacking and non-local variable declarations. Python 3.4.1 added proper Enum support to eliminate the older “make a class look like an Enum” that was found in previous versions. The good news is that the new Enums have been backported to Python versions 2.4-2.7 and 3.1-3.3.
The problem remains, though, that there’s an awful lot of Python 2 code out in the world that needs to be converted to Python 3. As we saw in my earlier article on popular Python frameworks, not all Web frameworks are Python 3-compatible yet. Web2py isn’t. Likewise, the very popular py2exe, which compiles Python scripts to run under Windows as .exes, is restricted to Python 2.6 and 2.7 (though there are alternatives such as cx_Freeze). Generally, there are still more libraries compatible with Python 2 than Python 3, but I believe the tipping point will come very soon.
Arguably, the move to Unicode is probably the one big reason for the change. If you’ve never had to deal with Unicode, take a look at Pragmatic Unicode, a series of slides by Ned Batchelder that shows why the change was needed. The short version is that the world runs on Unicode and the Python 2 implementation can cause problems unless you are very careful. In a way it’s analogous to moving away from C strings to the much safer C++ strings. Ironically, it was the C heritage of Python 2 that led to many of its quirks in the first place. The world has changed somewhat since Guido invented Python in 1991.
If you’re learning Python from scratch, you should go with Python 3. Some interesting stats from Nick Coughlan’s Python Notes show almost 38,800 packages, of which 26,500 claim compatibility with any version of Python. Seventy percent of the top 200 downloaded packages offer Python 3 support and it’s hard to imagine that figure not increasing with time.