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.
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
|
|
|---|---|---|
| Version | Python 2.x is legacy |
Python 3.x is the present and future of the language
|
| Standard Libraries | The final 2.x version 2.7 was released in mid-2010 and no more new versions will be released |
Python 3.x is under active development and all recent standard library improvements, for example, are only available by default in Python 3.x
|
| Unicode | In 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 Division | In Python 2.x, the resultant of division is always an integer value |
Whereas in Python 3 this has been corrected and the resultant will be a float value
|
| Print Function | print “hello” |
print (“hello”)
|
| Ordering Comparisons | Rules of ordering comparisons in Python 2 versions are complex in nature |
Python 3 has simplified the rules for ordering comparisons
|
| Iterations | It has both range and x range functions to iterate one object or through a list at a time |
Here 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 2print "IM Prep"print "IM Prep", "Python"
Output: IM Prep IM 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 3print ("IM Prep");
print ("IM Prep", "Python")
Output: IM PrepIM 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/3print "a =", a
# Printing in python 3
b = 10/3print ("b =", b)
Output: 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.
#Unicodein Python 2 & 3print " I am shukla U0001f44D"
Output in Python 2: I am shukla U0001f44D
Output in Python 3:am shukla
4) Xrange & Range
Python 2.x has both range and x range functions where as in Python 3, only range function is available and this is similar to x range in Python 2.x.Click here to understand the difference between range and x range 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.
