In this article, we will walk through the major key difference between Python 2 vs Python 3. Both the versions contrast to each other in many ways. Libraries and programs written on Python 2 might not work for Python 3. Note that many projects still use Python 2, although migrating to Python 3 is getting easier.

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.

Python 2

Published in late 2000, Python 2 signaled a more transparent and inclusive language development process than earlier versions of Python with the implementation of PEP (Python Enhancement Proposal), a technical specification that either provides information to Python community members or describes a new feature of the language.

Additionally, Python 2 included many more programmatic features including a cycle-detecting garbage collector to automate memory management, increased Unicode support to standardize characters, and list comprehensions to create a list based on existing lists. As Python 2 continued to develop, more features were added, including unifying Python’s types and classes into one hierarchy in Python version 2.2.

Python 2.7

Python 2.7 was published on July 3, 2010, and planned as the last of the 2.x releases. This compatibility support included enhanced modules for version 2.7 like unittest to support test automation, argparse for parsing command-line options, and more convenient classes in collections. Python 2.7, however, is considered to be a legacy language and its continued development, which today mostly consists of bug fixes, will cease completely in 2020.

Python 3

Python 3 is regarded as the future of Python and is the version of the language that is currently in development. A major overhaul, Python 3 was released in late 2008 to address and amend the intrinsic design flaws of previous versions of the language. Major modifications to Python 3.0 included changing the print statement into a built-in function, improve the way integers are divided and providing more Unicode support.

Additionally, many package libraries were only available for Python 2, but as the development team behind Python 3 has reiterated that there is an end of life for Python 2 support, more libraries have been ported to Python 3. The increased adoption of Python 3 can be shown by the number of Python packages that now provide Python 3 support, which at the time of writing includes 339 of the 360 most popular Python packages.

Python 2 VS Python 3

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 the handling of data. When Python 3 was launched initially it was slowly adopted because the migrations from Python 2 to Python 3 were not so smooth. Additionally, many Libraries and Packages were not compatible with Python 3.

The following are the most notable changes in Python 3.

Print

This is among some of the most crucial changes, in Python 2, the 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. As mentioned earlier Python 2.7 brought many changes to make the migrations smooth hence print() also works on Python 2.7

In Python 2.x print "Python is awesome"

In Python 3.x print("Python is awesome")

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 numbers, 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.

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 handles all strings as ASCII characters, which is an acronym of the 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 function to do operations on a string object. Now we can do string positional formatting like this:

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

Bonus tip: The _future_module

This is a Great package that 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 the future in your code, you first need to import it along with the feature you want to have in your program. In the below example, we are importing the division feature from Python 3 to Python 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.

Next Recommended Article: How To Run Hello World Program In Python

The article was published on September 10, 2020 @ 12:08 PM

Leave a Comment