Differences between Python 2.x and Python 3.x?


There is always a debate in the coding community on which python version was the best one to learn: Python 2.x or Python 3.x.

Below are key differences between pyton 2.x and python 3.x

1. The print function

In python 2.x, “print” is treated as a statement and python 3.x explicitly treats “print” as a function. This means we need to pass the items inside your print to the function parentheses in the standard way otherwise you will get a syntax error.

#Python 2.7

print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'some more text here'

Output

Python 2.7.6
Hello, World!
Hello, World!
text print some more text here
Python 3
import sys
print("Python version is %s.%s.%s" %sys.version_info[:3])
print('Hello, World!')

print("some text,", end="")
print('some more text here')

Output

Python version is 3.6.1
Hello, World!
some text,some more text here
>>> print "Hello"
Syntax Error: Missing parentheses in call to 'print'

2. Integer division

Python 2 treats numbers that you type without any digits after the decimal point as integers, which can lead to some unexpected results during division. For example, if you type the expression 3 / 2 in Python 2 code, the result of the evaluation will be 1, not 1.5 as you might expect. It is recommended to use either float(x) instead of using only x in your python 3 code (incase codebase port to python 2) or use from __future__ import division in your python 2 scripts.

#Python 2

print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0

Output

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

#Python 3.6.1

import sys
print('Python %s.%s.%s' %sys.version_info[:3])
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)

Output

Python 3.6.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

3. Unicode Strings

By default, Python 3 stores strings as Unicode whereas Python 2 requires you to mark a string with a “u” if you want to store it as Unicode. Unicode strings are more versatile than ASCII strings, which are the Python 2 default, as they can store letters from foreign languages as well as emoji and the standard Roman letters and numerals.

#Python 2

>>> print type(unicode('this is like a python3 str type'))
<type 'unicode'>
>>> print type(b'byte type does not exist')
<type 'str'>
>>> print 'they are really' + b' the same'
they are really the same

#Python 3

import sys
print('Python %s.%s.%s' %sys.version_info[:3])
print('strings are now utf-8 \u03BCnico\u0394é!')
print('Python %s.%s.%s' %sys.version_info[:3], end="")
print(' has', type(b' bytes for storing data'))
print('Python %s.%s.%s' %sys.version_info[:3], end="")
print(' also has', type(bytearray(b'bytearrays')))

Output

Python 3.6.1
strings are now utf-8 μnicoΔé!
Python 3.6.1 has <class 'bytes'>
Python 3.6.1 also has <class 'bytearray'>

“string” + b”bytes of data” will through an error.

>>> print ('they are really' + b' the same')
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
print ('they are really' + b' the same')
TypeError: must be str, not bytes

4. Raising Exception

Python 3 requires different syntax for raising exceptions. If you want to output an error message to the user, you need to use the syntax −

raise IOError(“your error message”)

Above syntax works on python 2 and python 3 both.

However, the following code works only in python 2, not in python 3

raise IOError, “your error message”

5. List Comprehension Loop Variables

In python 2, giving the variable that is iterated over in a “for loop” the same name as a global variable may leads to the value of the global variable being changed – which generally we don’t want. This issue has been fixed in Python 3, so you can use a variable name you already used for the control variable in your “for loop” without worrying about it leaking out and messing with the values of the variables in the rest of your code.

#Python 2
print 'Python', python_version()
i = 1
print 'before: i =', i
print 'comprehension: ', [i for i in range(5)]
print 'after: i =', i

Output

Python 2.7.6
before: i = 1
comprehension: [0, 1, 2, 3, 4]
after: i = 4

#Python 3

import sys
print('Python %s.%s.%s' %sys.version_info[:3])
i = 1
print('before: i =', i)
print('comprehension:', [i for i in range(5)])
print('after: i =', i)

Output

Python 3.6.1
before: i = 1
comprehension: [0, 1, 2, 3, 4]
after: i = 1

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements