Python is one of the most popular programming languages of this decade. The ever growing programming language is particularly applauded for its general-use functionality. Python can be used for web development, automation testing, web scraping, hacking etc. Especially at this time that there is a spike in the number of data scientists with python, the language has a lot of newbies now more than ever. 

If you are looking to start your journey in python, one of the decisions you must make is to determine whether to learn Python 2 or Python 3. In this post, you will learn how Python 2 differs from its Python 3 counterparts. By the end of this tutorial, you will understand which version to learn and why. 

Python 2

Python 2.0 was first made public in the year 2000 by a team called BeOpen Python Lab. Prior to that time, Python was managed by Rossum who did the most part in debugging and feature changes. Rossum however wanted Python to be accessible for all and help produce more programmers. It was with that paradigm that the Python Lab’s team birthed Python 2.0 and made it open source. 

Ever since, various versions of Python 2 have been released and the last being Python 2.7. It was however announced that there will be no further updates or improvement to Python 2 starting from the year 2020. 

History of Python 2

  • Python 2.0 was released 16th October 2000
  • Python 2.1 was released 15th April 2001
  • Python 2.2 waas released 21st December 2001
  • Python 2.3 was released 29th June 2003
  • Python 2.4 was released 30th November 2004
  • Python 2.5 was released 19th September 2006
  • Python 2.6 was released 1st October 2008
  • Python 2.7 was released 3rd July 2010

Why you may need Python 2.0

Although this version of Python is getting outdated, you may still need to learn the language for one of these reasons. 

  • In some companies, where their code was written with Python 2 and may not have been updated to Python 3
  • If you are working with configuration management tools such as ansible and puppet as a DevOps engineers, you may be required to work with both Python 2 and Python 3
  • If you are working on a project with a team, some of your dependencies or third party modules may not be compatible with Python 3 just yet. 

Python 3

The first version of Python 3, Python 3.0 was released in the year 2008. But this update was somewhat different from the usual updates that come from debugging in previous Python versions. Python 3.0 came with some sharp changes in the coding style and also had forward-compatibility. This means that it would only support other versions that are released after it. 

The syntax of Python 3 dealt with the problem of code redundancy and repetition, where a code can do the same task in various ways. Now in Python 3, there is one general most-suitable way of carrying out a task. This singular change proved to be extremely helpful for beginners that are looking to find their footing in writing python codes. 

History of Python 3

  • Python 3.0 was released 3rd December 2008
  • Python 3.1 ws released 27th June 2009
  • Python 3.2 was released 20th February 2011
  • Python 3.3 was released 29th September 2012
  • Python 3.4 was released 16th March 2014
  • Python 3.5 was released 13th September 2015
  • Python 3.6 was released 23rd December 2016
  • Python 3.7 was released 27th June 2018
  • Python 3.8 was released 14th October 2019
  • Python 3.9 was released 5th October 2020

The Key changes in Python 3

  • In python 2, print was a statement, while in Python 3, it has been changed to a built-in function. For example, the code print ‘Hello World’ is now changed to print(‘Hello World’). In Python 2.6 and 2.7 however, the built-in print function exists but has to be imported explicitly by using the expression from __future__ import print_function. 
  • In python 3, the input function behaves like the raw_input function of Python 2 such that it returns a string without evaluating the expression passed as argument. 
  • The integer division in Python 3 returns a float in situations where the result is not a whole number. In Python 2, the division of integers returns an integer no matter the case. For instance, 5/2 returns 2 in Python 2 while it returns 2.5 in Python 3.
  • In Python 3, the str and unicode types are unified for representing textual data. There exist another immutable type called the bytes type and the btyesarray type which are both used to represent arrays of beetles. 
  • In Python 3, the values of local variables remain unchanged. This is not the case in Python 2 as the values of local variables change in a for loop. 
  • In Python 2, the xrange function is used to perform iterations while in Python 3, the range function is used. 
  • Exceptions in Python 2 are enclosed in apostrophe in Python 2 while they are enclosed in parenthesis
  • The backward compatibility feature was removed in Python 3 which means you may find it difficult to move from python 3 to python 2. 

Final Verdict – To choose Python 3 or Python 2?

It is clear that Python 3 is the right version to use today. With the lack of further development in Python 2 from 2020, a lot of companies and Python frameworks are working to make their systems compatible with Python 3. See the Google Trend report that shows how Python 3 is being search with respect to Python 2

Python 2 or Python 3

Python 2 is fading gradually and will completely fizzle away in the coming years. It is wise to go with the latest trend and what is in demand. The only reason to learn Python 2 would be if a job demand requires specifically knowledge in Python 2. But that would rarely be the case. 

If you are not sure, just stick with Python 3.