What is new in Python 3 as compared to Python 2 ?

In this tutorial we are going to have a look at the various major differences implemented in Python 3 as compared to the previous iteration being Python 2; that you need to consider before switching your workflow between them.

Python 3 vs Python 2: Major differences

Why care about these updates at all

With this ever-growing generation of upgrades one after the another; it is mandatory right now to take care of the updated versions of the technologies you use in order to maintain your position in the industry.

Python is the primary language used for two of the current booming fields viz Machine Learning and Data Science is going under heavy development to meet the requirements of the users; as a result, new versions came in months after month.

Although many the times these are just some minor updates or bug fixes you don’t need to focus much on; but alongside major updates are also an integral part one being the transition from Python 2 to Python 3 that can literally break your code if you are not taking care of the changes that have had been executed in the new iteration.

 Important changes between Python 2 and 3

The new print() function :

In Python 3, the print statement has been over shadowed by the new print() function.  This would result in putting everything you need to output on the screen inside the parenthesis rather than putting it straight away after the print statement.

Old: print "The answer is", 2+2
New: print("The answer is", 2+2)

Putting everything as Argument as compared to special syntax with the print() :

With Python 3, we have pre-defined arguments to be used with the text in order to change the way the text outputs in the screen as compared to the special syntax method used in Python 2.

To print the text in the same line –

Old: print x,
New: print(x, end=" ")

To print a new line character –

Old: print
New: print()

The updated Division operator :

The division operator got revamped up with Python 3 and could your code provide with unexpected results when getting ported from Python 2 to Python 3 as it does not really raise any error to be noticed.



With Python 2

print 15/4
print -15/4

OUTPUT :

3
-4

With Python 3

print(15/4)
print(-15/4)

OUTPUT :

3.75
-3.75

That’s it for all the major changes that are for you to keep in mind.

Feel free to drop any doubts in the comments section below.

Further, check our other posts as well :

Leave a Reply

Your email address will not be published. Required fields are marked *