For coders looking to get into the Python programming language, the issue at hand that has become increasingly important is which version of Python to learn—Python 2 or 3? The language, which supports scripting and object-oriented styles has been around since the 80s and has been continuously evolving. So, let’s look at the different styles of Python and what differences might sway a coder to one version or the other.
Overviews of both Python 2 and Python 3
Before we discuss which option is the best for you, we need to look into the background of each of the versions of Python currently being used in industry and the features they present.
Python 2 became more transparent and inclusive, implementing something called PEP or Python Enhancement Protocol. This is a specification that provides information to community members or describes a new feature of the language. It provided a way around some “black-box” changes that happen in other languages as the header files and other parts of the language are changed.
In addition, garbage collection and memory allocation were improved, and support for Unicode to standardize characters for worldwide use were added. The ability to create a list based on existing lists was added as well. More features continued to be added, creating one hierarchy known as Python 2.2. This version is considered a legacy version, but is available from Python at legacy.python.org. A poll at Python.org shows that 97% of coders have written in Python 2.x.; these numbers indicate that there is still a strong base of Python 2 developers.
Python 3 is the version of the language that is currently in development. Overhauled from Version 2, Python 3 was released in late 2008 to address design flaws of previous versions of the language. The focus of Python 3 was to clean up the code and remove redundancy, whereas previous versions had more than one way to do a task, creating confusion.
Python 3.0 made the
function a built-in construct of the language. In addition, it improved the way in which integers were handled. It was slowly adopted because Python 3 due to its upgrades was not backwards compatible with Python 2. This required people to decide as to which version to use.
Also, many package libraries in common use were available only for Python 2, which made the migration to Python 3 less attractive. A list of available libraries can be found here. The problem then became that the Python team began to stress that Python 2 does have an end of life, and support will only be available through the community and not through the Python development team. This pushed many people into the Python 3 paradigm. Python 3 has almost caught up to the number of available package libraries in Python 2. Python 3 is available in its latest release at www.python.org/downloads as version 3.4.6 at the time of writing.
Following the 2008 release of Python 3.0, Python 2.7 was published as the last of the 2.x releases. Python 2.7 was designed to make it easier for Python 2.x users to migrate to Python 3 by providing some measure of compatibility between the two. Because of this Python 2.7 has persisted as a very popular choice for programmers due to its compatibility with many robust libraries. When we talk about Python 2 today, we are typically referring to Python 2.7 as that is the most frequently used version. However, Python 2.7 is a legacy language and will lose all official support in 2020. Currently most support is bug fixes and not extensions to the language. Python 2.7 can be downloaded from http://www.python.org/downloads.
Key Python Version Differences
The most important differences between python 2 and 3 are:
- Vastly Different options in Libraries
- Directories do not have init.py in Python 3.X
- Larger Community support for Python 3
- Python offers larger 3rd party Modules
Most of the differences you will see between the three versions of Python (really 2, as most Python 2 users are using the transitional version 2.7) are syntax-related. The print command has gone from a keyword to a function, so what used to be
print ‘Hello, world’
Division with integers has changed as well. In the Version 3 vernacular, dividing an integer with a remainder returns a floating point. Previously, dividing integers returns integers with no remainder. So, we went from this:
int a = 5 / 2
int a = 5 / 2
In order to get the remainder in 2.7, we must do modulus math (get exclusively the remainder with the % operator) in order to get both the dividend and remainder. Or, we can add a decimal point to the version 2.7 version and divide 5.0 by 2.0 to get the expected 2.5.
One last difference between Python 2.7 and 3.0 is that version 2 of Python used ASCII characters, whereas Version 3 uses Unicode characters. To use Unicode characters in Version 2, the character “u” precedes strings, which casts the data type to Unicode. Version 3 uses Unicode by default. To have backwards compatibility, you must use the u character before strings to make the code portable.
Should I learn Python 2.7 or 3.5 first as a Beginner?
Based on the declining support for version 2, as well as the added benefits from upgrades, it is intuitive for a new coder to choose Version 3 of Python. However, if a job requires Python 2 capabilities, that would be a compelling reason to learn the syntactic and function differences between the two.
If you are just picking up a language, we recommend to learn Python 3 as its support will continue well into the future and is being used extensively. It is not difficult to go back and learn the subtle differences between the two languages should you need to use 2.7 at some point in the future. Python and its development tools and environment, as well as community support are available at http://www.python.org including a place to find jobs that require Python at http://wiki.python.org.