Python 2 Vs Python 3 | Which Should You Learn In 2019?

The considerable difference in the versions of Python makes it difficult for programmer/beginners to understand which among Python 2 and 3 is preferable. However, this article on Python 2 vs Python 3 will help you get a better idea.

                python 2 vs python 3

Python 2 Vs Python 3

If asked which among Python 2 and 3 should a beginner learn, then absolutely we would suggest you learn the latest version i.e Python 3. If you haven’t downloaded it yet, then here is how you can quickly do it now.

The software industry has also started using Python 3 adversely soon after it was declared that Python 2 will no longer be supported after 2020. Yes! You heard it right. You will also no longer receive support for Python 2 after 2020 and hence effort to learn it at this point in time doesn’t make sense. So quickly get started with using Python 3.

However, if are still curious to know the differences, then here they are: 

 Python 2.x Python 3.x
VersionPython 2.x is legacyPython 3.x is the present and future of the language
Standard LibrariesThe final 2.x version 2.7 was released in mid-2010 and no more new versions will be releasedPython 3.x is under active development and all recent standard library improvements, for example, are only available by default in Python 3.x
UnicodeIn Python 2.x, all the strings are stored as ASCII values by default and hence to store Unicode string values you need to define them with “u”But in Python 3, all strings are stored as Unicode by default
Integer DivisionIn Python 2.x, the resultant of division is always an integer valueWhereas in Python 3 this has been corrected and the resultant will be a float value
Print Functionprint “hello”print (“hello”)
Ordering ComparisonsRules of ordering comparisons in Python 2 versions are complex in naturePython 3 has simplified the rules for ordering comparisons
Iterations It has both range and xrange functions to iterate one object or through a list at a timeHere range does what xrange does in Python 2.7 and xrange function is not available

Some of the above differences are explained in detail below.

1) Print Statement -> Print Functions

In Python 2, print is a statement. To print a word, the syntax used is print “word” and to print multiple words, the syntax is print “word1”, “word2”

# Printing in python 2
print "FACE Prep"
print "FACE Prep", "Python"
FACE Prep Python

Whereas in Python, print is a function and the syntax is print (“word”). If you emit the parenthesis, then you will end up getting an error. 

# Printing in python 3
print ("FACE Prep");
print ("FACE Prep", "Python")
FACE Prep Python
2) Integer Division

In Python 2, the division of two integers will by default result in an integer value. Whereas in Python 3, the result is a float value by default.

# Printing in python 2.7
a = 10/3
print "a =", a

# Printing in python 3
b = 10/3
print ("b =", b)
a = 3
b = 3.3333333333333335
3) Unicode

In Python 2, strings are stored as 8-bit ASCII values by default. To store them as unicodes, you explicitly need to mention “u” before the string. Whereas in Python 3, they are stored as Unicode by default. Unicode is better as it is more versatile than ASCII. Unicode strings will allow you to store symbols, emojis, foreign language letter, roman letters etc. 

# Unicode in Python 2 & 3
print "amar shukla \U0001f44D"
Output in Python 2: amar shukla \U0001f44D
Output in Python 3: amar shukla 👍
4) Xrange & Range

Python 2.x has both range and xrange functions where as in Python 3, only range function is available and this is similar to xrange in Python 2.x. Click here to understand the difference between range and xrange with examples.

These are some of the major changes in Python 3 in comparison with Python 2. However, if you want to know all the changes, then check out complete Python 2 and 3 differences.