What are the differences in between python 2.x and python 3.x versions?



In this article, we look at a few important differences between python 2.x and python 3.x with examples.

  • The print function
  • Integer division
  • Unicode Strings
  • Raising Exception
  • _future_module
  • xrange

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

The print function

The print keyword from Python 2.x is substituted with the print() function from Python 3.x in this case. Because the interpreter analyses it as an expression, parentheses work in Python 2 if space is supplied after the print keyword.

Example

This is an example to understand the usage of print function.

print 'Python' print 'Hello, World!' print('Tutorialspoint')

Output

When the above code has complied on both the versions, the outputs are shown

Python 2.x version

Python 
Hello, World! 
Tutorialspoint

Python 3.x version

File "main.py", line 1
    print 'Python' 
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean 
print('Python')?

The below line of code can be used to print a statement in python.

print('Tutorialspoint')

Output

The outputs obtained in each versions are given below.

Python 2.x version

Tutorialspoint

Python 3.x version

Tutorialspoint

Integer Division

Python 2 interprets numbers without any digits after the decimal point as integers, which might result in some unexpected division results. If you type 5 / 2 in Python 2 code, the result of the evaluation will be 2, not 2.5, as you may expect.

Example

This example depicts the working of integer division in python.

print ('5 / 2 =', 5 / 2) print ('5 // 2 =', 5 // 2) print ('5 / 2.0 =', 5 / 2.0) print ('5 // 2.0 =', 5 // 2.0)

Output

The outputs obtained in both versions are as follows.

In python 2.x version

5 / 2 = 2.5
5 // 2 = 2
5 / 2.0 = 2.5
5 // 2.0 = 2.0

In python 3.x version

5 / 2 = 2.5
5 // 2 = 2 5 
/ 2.0 = 2.5 5 
// 2.0 = 2.0

Unicode

In Python 2, an implicit str type is ASCII. However, the implicit str type in Python 3.x is Unicode.

  • In Python 2 you have to mark a string with a “u” if you want to store it as Unicode.

  • In Python 3 you have to mark a string with a “b” if you want to store it as Byte code.

Example 1

The difference in Unicode in python 2 and python 3 can be seen below.

print(type('default string ')) print(type(b'string which is sent with b'))

Output

The outputs obtained for each of the different version are as follows.

In python 2 version

<type 'str'> 
<type 'str'>

In python 3 version

<class 'str'> 
<class 'bytes'>

Example 2

Another example for the Unicode in the different versions of python 2 and 3 are as follows.

print(type('default string ')) print(type(u'string which is sent with u'))

Output

The outputs obtained are as follows.

In python 2 version

<type 'str'> 
<type 'unicode'>

In python 3 version

<class 'str'>
<class 'str'>

Raising Exception

Raising exceptions in Python 3 requires a changed syntax. You must use the syntax if you wish to display an error message to the user.

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”

_future_module

This isn't really a difference between the two versions, but it's important to note. The __future__ module was created to aid with the migration to Python 3.x.

We can use _future_ imports in our code if we want Python 3.x compatibility in our Python 2.x code.

Example

The following code can be used to import and use the

from __future__ import print_function print('Tutroialspoint')

Output

The output obtained is given below.

Tutorialspoint

Advertisements