Signup/Sign In

Python 2 vs. Python 3

If you are reading this in 2020, then there is no point even discussing this unless and until you work on some project which is still running on some version of Python 2.x.

When we say Python 2.x we mean Python 2.7 and when we say Python 3.x we mean Python 3.7 version.

Python released the Python 3 version of the programming language in 2008. There was an initial hesitation around adoption of Python 3 but not anymore.

Almost all the libraries/modules of python have been moved to python 3 or have been made compatible to work with python 3.x version.

So now, there is no point using Python 2.x version unless and untill you are working on some legacy code in your application which still runs on Python 2.x version.


Important Differences between Python 2 and Python 3

Although there are many changes in the newer version of the language i.e. in Python 3 as compared to Python 2. We will be covering the ones which are the most important ones which can cause issues if you are porting your code form Python 2.7 (or any other 2.x version) to Python 3.x version.

New print() method syntax

Yes, this is one of the most visible change as the print statement is used a lot while you code in python.

In python 2.x, there was no need of adding parentheses after the print keyword to enclose the text to be printed, but from 3.x version of python, the correct syntax is print("TEXT TO BE PRINTED").

Let's see some code example to understand:

# In python 2.x
print "Text to be printed"

Text to be printed

While in case of Python 3.x version, the same code will become,

# In python 3.x
print("Text to be printed")

Text to be printed


Changes in Division (/) operator

The division operator in Python 2.x returned an integer result value whereas in Python 3.x now you get a float result value which is more precise.

Let's see some code example to understand:

# In python 2.x
print 5/2

2

While in case of Python 3.x version, the same operation will return a float value,

# In python 3.x
print(5/2)

2.5


Function xrange() no longer supported

In Python 2.x if we wanted to generate a list of numbers within a range we could either use the range function which returns a list for example range(2) will return the list [0,1] or use the xrange() function which doesn't returns a list but returns an iterator object on which you can iterate and get the range of numbers. In simpler words, rather than creating a list of numbers at once and returning it, the xrange() function generated the numbers of range only when it is needed i.e. when we use it using a loop.

In case of Python 3.x, there is no xrange() function. The range() function does what xrange() used to do.

You will understand this change better after you know the concept of Iterators and Iterables in python.


Error Handling

There is a small change in the syntax in the usage of try and except blocks for error handling in python.

Let's see some code example to see the change first,

# In python 2.x
try: 
    # some statement that may cause error 
except SomeError, err: 
    print err, "Error Occured"

While in case of Python 3.x version, in the except block, we now use as keyword with the error type and its variable object,

# In python 3.x
try: 
    # some statement that may cause error 
except SomeError as err: 
    print(err, "Error Occured")

This change is to make the syntax more readable and user friendly. If you are new to Error and Exception handling in Python, you can learn about it in the later part of this tutorial series.


Some more changes worth mentioning

Here are some more changes we would like to mention:

  1. In Python 2.x the implicit or default string encoding is ASCII whereas in Python 3.x it is Unicode.
  2. Many python libraries and modules which were earlier available for Python 2.x version may not be available for Python 3.x, but most are.
  3. With Python 3.x, the syntax of the language has become more user friendly.

Conclusion

We would recommend you to use Python 3.x version for learning purpose. Although in some of our tutorials you may find print statement in old syntax, but now you know what is the correct syntax for print() in Python 3.x, so edit the code ;)