Python2 vs Python3 – A Brief Comparison

Feature Img Python2 Python3

Hello learner! You must have heard about Python2 vs Python3 where some use the 2 versions but some the 3. Today let us know the difference between the two versions of Python.

Major Differences Between Python2 vs Python3

Let’s get right into the differences of both Python 2.x and Python 3.x by understanding some of the most commonly used functions and how they are different in both the versions.

1. The print statement

Python VersionSyntax
Python2print “I am Python2 version”
Python3print (“I am Python3 version”)

In general, the output for both the syntax mentioned above is exactly the same. But the use of parenthesis in Python3 makes it easier to read for the user.

2. input statement

All programs require user input and it only makes sense to add it to the list here. Let’s see how we use the input method in Python2 and Python3.

Python VersionSyntax
Python2raw_input(): For strings
input(): For integer
Python3input(): For all kinds of inputs required

3. variables in print statement

How do we use variables in the print statement using the format string method between Python2 vs Python3?

Python VersionSyntax
Python2msg = “Hello”
print (“The message entered is % ” % msg)
Python3msg = “Hello”
print (“The message entered is {0} “) .format(msg))

4. Error Handling

In python3 the programmer requires adding as as an extra keyword in the except block.

Python VersionSyntax
Python2try:
//code
except <ERROR>,err:
//code
Python3try:
//code
except <ERROR> as err:
//code

5. Division operation in Python

In the case of Python2, division operation results in an integer. On the other hand, Python3 returns a floating value after the division operation.

6. Iteration Fuction

In Python2, xrange() is used for the iterations while in Python3 the new and advance range() function is used for iterations.

Which is better between Python2 vs Python3?

Now most developers are creating libraries strictly compatible with Python 3. It is also easier to code in and understand than Python2

Also, in Python3, strings are stored in form of Unicode which is more versatile than ASCII codes used in Python2. And lastly, Python3 removes development conflicts as it allows typing which Python2 doesn’t support.

In addition to this, Python 3 supports all modern programming like Artificial Intelligence, machine learning, and data science concepts.

To put it into simple words : Python2 is the past and Python3 is the Future!

Conclusion

When it comes to which version to prefer, Python2 or Python3, we can conclude that Python 3 is a straight winner. Also if you are a new programmer, I would advise you to select Python3.