Japneet Singh Chawla

Jul 7, 2020

4 min read

Python 2 Vs Python 3 Which should you select

There has been a huge debate on this topic, “ Whether you should learn Python 2 or Python 3”. If you are also in this dilemma this post is for you.

The first thing you should understand is that if an upgrade is done in anything in the world it is mostly for improving that thing in terms of experience, speed, efficiency, etc.

So, this upgrade of Python language is also done to improve the features and functionality of the language but than, Why is this debate all around about Python 2 and Python 3, why can’t people just accept this new update rather than debating.

To understand this you will have to think deeply and understand that Python has been there for nearly 29 years now(created in 1991), there are large number of legacy systems which are built on Python 2 and there are some feature of Python 3 which are not backward compatible with Python 2.

For Example:

The scenario of a simple print statement. In Python 2 print was considered as a statement and in Python 3 it is treated as a function, which leads to change in the Syntax of the print statement in Python.

Python 2

print "Welcome to The Data Singh"

Python 3

print("Welcome to The Data Singh")

This small change can lead to rewriting a large number of lines in a big project.

Now let’s understand some more differences between Python 3 and Python 2

Strings as Unicode

Python 3 handles strings as Unicode unlike python 2 which treats it like ASCII which can handle a limited number of characters.

I have personally suffered a lot due to ASCII handling of python 2 so I will say this feature is a very important and required feature especially if you want to work in the field of NLP.

input Vs raw_input

Python 2 had two functions input() and raw_input() for taking inputs from the user.

input() takes user input and tries to convert it into a suitable form. For example if the user enters an integer, input will convert the data type to int. On the other hand raw_input() keeps every input as a string and the user can convert it into any other form using typecasting.

Python 3 has just one function input() which takes user input as a string, we can then convert the input to any form.

range vs xrange

If you are familiar with Python you might be aware of these functions. For the new users, range function generates a list, and xrange function yields a generator.

Understanding generators is out of the scope of this post.

Python 2

You can see range generates a list and xrange generates a xrange object

Python 3

Python 3 has just one function i.e range which generates a range object.

Now some facts about the versions

Python 3 is the future of python also active support for python has stopped from January 2020. If anyone is starting a new project in Python it is getting developed in Python 3 but if anyone if maintaining a legacy system they are using Python 2.

All the new libraries which are getting created in python are based on Python 3 many of which are not compatible with Python 2, so if you are planning to work on python 2, you might miss some important and useful libraries.

Many of the major companies like Facebook and Instagram have moved to Python 3 so you now know the reasons to learn or move to Python 3.

If you have any question just comment below also check my other blogs posts:

Why Should you learn Python in 2020

Writing Instagram Bot with Python

Extracting text from PDF using Python

Writing a Twitter Bot in Python

Also, have a look at my channel The Data Singh on youtube