Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Key Difference Between Python 2 and Python 3

5 min read

Python is a widely used high-level open source dynamic programming language used for general-purpose programming, created by Guido Van Rossum and first released in 1991. The name was inspired by the British comedy group Monty Python.

Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, functional programming, and procedural styles.

Python sports a remarkably simple, readable, and maintainable syntax and have many comprehensive standard libraries which made it the language of the century.

Although it is a general-purpose language, Python is often called a scripting language because it makes it easy to utilize and direct other software components.

In 2008 the Python team rolled out the most significant update of all time, to overhaul all the fundamental flaws of the previous version. This was the release of Python 3.

This update made a big split between Python 2 and Python 3.

There are two major versions of Python which are currently in active use:

Python 3.x is the latest version and is under active development which was first released in 2008.

Python 2.x is the legacy version and will receive security updates until 2020. No new features will be implemented. Note that many projects still use Python 2, although migrating to Python 3 is getting easier.

Both the versions contrast to each other in many ways. Libraries and programs written on Python 2 might not work for Python3. In this article, we will walk through all the major changes of this update.

When Python 3 was launched initially it was slowly adopted because the migrations from Python 2 to Python 3 was not so smooth. Additionally, many Libraries and Packages were not compatible with Python 3.

However, things are changed now in 2019 when I am writing this almost all the all the popular Python packages has been ported to Python 3. Python security updates are about to come to an end, and soon the world will move to Python 3.

Almost every enterprise is porting their Python 2 projects to Python 3.

Python 2.7

Following the 2008 release of Python 3.0, Python 2.7 was published on July 3, 2010 and planned as the last of the 2.x releases. The intention behind the launch of Python 2.7 was to make the migration from Python 2 to 3 smoother by providing a measure of compatibility between these two.

The compatibility includes adding extra features and new modules such as unittest to support test automation, argparse for parsing command-line options, and more convenient classes in collections.

Python 2.7 holds a unique position between Python 2 and 3, when we talk about Python 2. We are referring to Python 2.7 as it is the most frequent version which is currently in active use.

Python 3 VS Python 2

Python 2 and Python 3 do have some significant changes but they are not entirely different languages. However, there are some significant changes in the syntax of some functions and handling of data.

Following are the most notable changes in Python 3.

Print

This is among some of the most crucial changes, in Python 2, print was treated as a statement while in Python 3 it is treated as a function, which implies it needs arguments inside parenthesis to execute.

Example -

In Python 2.x

print "Python is awesome"

In Python 3.x

print("Python is awesome")

As mentioned earlier Python 2.7 brought many changes to make the migrations smooth hence print() also works on Python 2.7

Division with Integers

In Python 2, any number written without a decimal is treated as an integer by default. This may sound handy but it isn't this created a problem which Python developers weren't aware of in the beginning.

Sometimes when we do a simple division between two integer number for instance, let's say 7/2 we are expecting the output to be 3.5 right?

In python 2 you won't get a float output on integer operation, so you will get the floor value of the output, which in this case is 3. In order to get the exact answer in you have to provide float inputs like 7.0/2.0.

This was obviously not a reliable way of handling data.

However Python 3 came with a fix, in Python 3 you will get a float output even for integer inputs.

print(7/2)
3.5

Unicode and Strings

A string is nothing but a sequence of characters. In Python, there are two different ways to handle a String.

Python 2 by default handle all strings as ASCII characters, which is an acronym of American Standard Code for Information Interchange. ASCII is not the best way of handling string because it is limited to a couple of hundreds of character ASCII is not a very flexible method for encoding characters, especially non-English characters.

A more reliable solution is Unicode encoding, which supports over 128,000 characters non-English characters and emojis.

In Python 2, we need to add the prefix 'u' in order to make any string Unicode string like this, u"I am an Unicode String"

However, Python 3 now treats all strings as a Unicode string by default without any 'u' prefix, which proves to be more efficient.

Input Function

In Python 2, there were two input functions, raw_input() and input().

Programmers have to think twice before using input() function because it might screw things up. Previously this function tends to evaluate the input on its own. For example, if you want only a string input but the user types 69 now Python will think, "Oh! that's an integer" and Python 2 will convert it to an integer instead of a string which might end up crashing your code or do things which you were not expecting.

In Python 3 there is only one function for input which is, input(), and now it doesn't manipulate the input, this was a much-needed change.

A new way of string Formatting

In Python 2 string formatting was done with the (%) operator similar to c language, Python 3 brought the format() function to do operations on a string object.

Now we can do string positional formatting like this:

>>> 'Hello, {}'.format(name)
'Hello, Jhon'

However, later this feature was included in Python 2.7 release.

Bonus tip

The _future_module

This is Great package which helps you to use features of the newer version of Python while having an older release of Python. In simple words, you can use Python 3 features in Python 2 using this module.

In order to use future in your code, you first need to import it along with the feature you want to have in your program.

In below example, we are importing the division feature from Python 3 to Python 2,

from __future__ import division
print 7/2

And this will return you the exact float output 3.5

Conclusion

Python is a versatile and well-documented programming language to learn.

If you are thinking of starting off your journey with Python then it is advisable to go for Python 3 because Python 2 is getting obsolete and there won't be Python 2.8 this is the end of the legacy version of Python. Python team has already announced there won't be any security update after 2020 for Python 2.

I hope you make the right choice.

Have Questions? Feel free to drop a comment below!


PYTHON

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read