In this blog, we will be learing the differences between python 2 and python 3. These differences are important as we go further in the technicalitis of python for data scientist.
- Division Operator
- Print Function
- Unicode
- Xrange
- Error Handling
- __future__module
Division Operator
If you try to execute the code written in python 3.x and run in python 2.x version, it can cause an undetected error as the later version does not work the same as the former one. Here, one such case is integer division. We will recommend you to use floating values when dividing an integer.
print 7 / 5
print -7 / 5
”’ Output in Python 2.x
1
-2
Output in Python 3.x :
1.4
-1.4 ”’
print function
You will see the most significant change in this function only as the print command has been changed to a function from the statement in the earlier version.
print ‘Hello, Kathan’ # Python 3.x doesn’t support
print(‘How are you Kathan Suri’)
”’ Output in Python 2.x :
Hello, Kathan
How are you Kathan Suri
Output in Python 3.x :
File “a.py”, line 1
print ‘Hello, Kathan’
^
SyntaxError: invalid syntax
”’
You can see that in python3.x, the print statement cannot be executed but in python2.x the print function can be, it shows a syntax error if we try to do so.
Unicode
You can also see that in python2.x the implicit str type is ASCII but in python3.x the implicit str type is Unicode.
print(type(‘default string ‘))
print(type(b’string with b ‘))
”’ Output in Python 2.x (Bytes is same as str)
<type ‘str’>
<type ‘str’>
Output in Python 3.x (Bytes and str are different)
<class ‘str’>
<class ‘bytes’>
”’
Python 2.x also supports unicode.
print(type(‘default string ‘))
print(type(u’string with b ‘))
”’
Output in Python 2.x (Unicode and str are different)
<type ‘str’>
<type ‘unicode’>
Output in Python 3.x (Unicode and str are same)
<class ‘str’>
<class ‘str’>
”’
xrange:
We have two functions in python2.x, one is range() and the other is xrange(). The former one is used to get of the list of elements while the later one to get the elements without being in the list. Now, the xrange: functionality has been added to the range function in python3.x.
For example:
for x in xrange(1, 9):
print(x),
for x in range(1, 9):
print(x),
”’ Output in Python 2.x
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Output in Python 3.x
NameError: name ‘xrange’ is not defined
”’
Error Handling
Only one change was made in the newer version of this language, ‘as’ keyword has been added to the exception part of the block.
try:
trying_to_check_error
except NameError, err:
print err, ‘Error Caused’ # Would not work in Python 3.x
”’ Output in Python 2.x:
name ‘trying_to_check_error’ is not defined Error Caused
Output in Python 3.x :
File “a.py”, line 3
except NameError, err:
^
SyntaxError: invalid syntax ”’
try:
trying_to_check_error
except NameError as err: # ‘as’ is needed in Python 3.x
print (err, ‘Error Caused’)
”’ Output in Python 2.x:
(NameError(“name ‘trying_to_check_error’ is not defined”,), ‘Error Caused’)
Output in Python 3.x :
name ‘trying_to_check_error’ is not defined Error Caused ”’
__future__module:
This is basically not a difference between the two versions but a feature in the newer one. It helps in migration. You can import the features of python3.x in python2.x by using the __future__ import in your code.
In the following example, the integer division used in the 3rd version has been used in the 2nd one.
# In below python 2.x code, division works
# same as Python 3.x because we use __future__
from __future__ import division
print 7/5
print -7/5
Output:
1.4
-1.4
Example 2 :
from __future__ import print_function
print(‘PST Analytics’)
Output: PST Analytics
To download python 2 and python 3, click here,so that you can start learing python for data scientist.
