Learn Python the Hard Way: Python 2 Vs Python 3
In this tutorial, we are going to learn about python the hard way by comparing python 2 vs python 3 and also understand the main reasons why it is important to have a reasonable difference between the two.
Most programming languages update themselves periodically to help developers enhance their coding abilities as well as solve the challenging queries of the growing market. And whenever a new version of a programming language is released it almost carries the same features and tools of the previous version while adding new features to the latest version. However, python 2 and python 3 are not that similar. In fact, there were huge changes made in the latest python 3 version and a bunch of features of python 2 were dropped in python 3.
Important differences between python 2 and python 3 are:
The Print Function
Python 2 uses print as a statement to print “anything” to print on the screen.
>>>print 3 3 >>>print 'Hello Learners' Hello Learners
In python 3 print has been replaced by print function print(). So, you will need extra parantheses in order to use the print function.
>>>print(3) 3 >>>print("something") something
xrange() and range() functions
Python 2.x uses the xrange() function to create iterable objects, whereas in python 3 xrange has been replaced by range() function.
xrange() in python 2.x
for x in xrange(1,10): print x
Output:
1 2 3 4 5 6 7 8 9
xrange() in python 3:
for x in xrange(1,10): print x
Output:
NameError: name 'xrange' is not defined
raw_input() and input() function
Python 2 uses raw_input() to accept the input from the user whereas in python 3, raw_input has been replaced by input() function.
Learn Python The Hard Way
To understand the key differences between python 2 and python 3 and solve exercise in depth, you can get this book by Zed Shaw called Learn Python the Hard Way
The book covers main ideas and references as to why python 3 is far better than python 3 by taking a step-by-step approach that takes you through each instructional exercise and also contains video lessons for students who prefer a live practicing session.