Python 2 versus Python 3
Written by
If you were asking this question a few years ago, then it would be valid. But now, as we approach 2019, this question is a no-brainer – Clearly, Python 3 wins by a landslide.
By 2020, developers will stop maintaining Python 2.
Python 2 was first released in 2000, and its latest version was released in 2008, while on the other hand Python 3 was released in 2008.
Though many developers have moved on to Python 3, we shouldn’t forget the fact that most of the old code of all the Python-based companies was written in Python 2, making it the legacy code. So we shouldn’t forget its importance.
Python 2 Vs Python 3:
Let’s look into some important differences between Python 2 and Python 3- (Just keep this stuff in mind, we will go deep into most of these topics later on)
1.Print function – In Python 3, the print function is defined as follows-
print('This is Python 3!')
On the other hand, the same thing is written in Python 2 as follows –
print 'This is Python 2!'
See the difference? Yeah, Python 3 requires a pair of parentheses for its print function to work.
2. String type – In Python 3, the implicit string type is Unicode, while in Python 2 it’s ASCII.
In a way, this means that in Python 3, bytes and the str type are different, while in Python 2 they are the same. This can be found out by using the type function.
3. xrange of Python 2 – xrange() has become extinct in Python 3 and doesn’t work in it. Instead, we use range() function, which is the same as xrange() of Python 2. (We will see the range() function in the loops section)
4. Integer division– In Python 2, the ‘/’ operator performs integer division i.e. if our operands are integers, but the resultant is a floating-point number, we will get an integer with the decimal value rounded off instead of the floating-point value.
On the other hand, the ‘/’ operator does pure division in Python 3. So, 5/4 in Python 2 will be 1, but in Python 3 it will be 1.25.
5. There are some more differences, but these are not relevant for these tutorials. For example – the _future_ module, error handling, automated migration etc. These are a bit advanced topics.