Important differences between Python 2.x and Python 3.x with examples



If we are porting our code or executing python 3.x code into python 2.x, it can be dangerous if integer division changes go unnoticed (since this does not cause any error). It is preferable to use a float (like 7.0 / 5 or 7 / 5.0) to get the expected result when we wrap our code.

print 7 / 5

print - 7 / 5  

 
"" "
Exit to Python 2.x
1
- 2
Output in Python 3.x:
1.4
- 1,4

 
# See Link below for details 
# https:// www.python.engineering/division-operator-in-python/amp/
"" "

print function

This is the most famous change. This replaces the print function in Python 2.x with the print () function in Python 3.x, meaning that printing in Python 3.x requires an extra pair of parentheses.

print `Hello, Geeks`   # Python 3.x does not support

print ( `Hope You like these facts ` )

  
"" "
Output in Python 2.x:
Hello geeks
Hope you like these facts

 
Output in Python 3.x:
File & quot; a.py & quot;, line 1

print Hello Geeks

^

SyntaxError: invalid syntax

 
Refer to the link below for details
https://www.python.engineering/g-fact- 25-print-single-multiple-variable-python / amp /
"" "

As we can see, if we use parentheses in Python 2.x, then this is not a problem, but if we don`t use parentheses in Python 3.x, we get SyntaxError.

Unicode:

In Python 2, the implicit type str — ASCII. But in Python 3.x the implicit type str is Unicode.

print ( type ( `default string` ))

print ( type (b `string with b` ))

 
"" "
Output in Python 2 .x (matches str in bytes)
& lt; type & # 39; str & # 39; & gt;
& lt; type & # 39; str & # 39; & gt;

 
Output in Python 3.x (bytes and str are different)
& lt; class & # 39; str & # 39; & gt;
& lt; class & # 39; bytes & # 39; & gt;
"" "

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)
& lt; type & # 39; str & # 39; & gt;
& lt; type & # 39; Unicode & # 39; & gt;

 
Output in Python 3.x (Unicode and str are the same)
& lt; class & # 39; str & # 39; & gt;
& lt; class & # 39; str & # 39; & gt;
"" "

xrange:

xrange () in Python 2.x does not exist in Python 3.x. In Python 2.x, range returns a list, i.e. range (3) returns [0, 1, 2], and xrange returns an xrange object, that is, Xrange (3) returns an iterator object that works like a Java iterator and generates a number as needed. 
If we need to repeat the same sequence multiple times, we prefer range () because range provides a static list. xrange () rebuilds the sequence every time. xrange () does not support slices or other list methods. The advantage of xrange () is that it saves memory when the task is to iterate over a large range.

In Python 3.x, the range function now does what xrange does in Python 2.x, so, to keep our code portable, we could stick with using range instead. So, the range function in Python 3.x —  this xrange from Python 2.x.

for x in xrange ( 1 , 5 ):

print (x),

 

for x in range ( 1 , 5 ):

print (x),

  
" ""
Exit to Python 2.x
1 2 3 4 1 2 3 4

 
Exiting Python 3.x
NameError: name & # 39; xrange & # 39; undefined
"" "

Error handling:

Both versions there are small changes in error handling. In python 3.x, the “as” keyword is required.

try :

trying_to_check_error

except NameError, err:

  print err, ` Error Caused`   # Will not work in Python 3.x

 
"" "
Output in Python 2.x:
name & # 39; ring_to_check_error & # 39; undefined Error caused

 
Output in Python 3.x:
File & quot; a.py & quot ;, line 3

except for NameError, err:

^

SyntaxError: invalid syntax
"" "

try :

  trying_to_check_error

except NameError as err: # & # 39; as & # 39; required in Python 3.x

print (err, `Error Caused` )

  
" ""
Output in Python 2.x:
(NameError ("name_update_check_error" is undefined "," An error was caused ")

  
Output in Python 3.x:
name & # 39; ring_to_check_error & # 39; undefined Error caused
"" "

_future_module:

This is basically not the difference between the two versions but it is useful to mention here. The idea of ​​the __future__ module — help in m games. We can use Python 3.x
If we plan to support Python 3.x in our 2.x code, we can use _future_, by importing it into our code.

For example, in the Python 2.x code below, we are using Python 3.x integer division behavior using the __future__ module

# In the following Python 2. x, separation works
# the same as Python 3.x because we are using __future__

from __ future__ import division

 

print 7 / 5

print - 7 / 5

Output:

 1.4 -1.4 

Another example where we use parentheses in Python 2.x using the __future__ module

from __ future__ import print_function 

 

print ( `GeeksforGeeks` )

Output:

 Python.Engineering 

Refer to this for more details on the __future __ module.

This st This article is provided by Arpit Agarwal . If you like Python.Engineering and would like to contribute, you can also write an article and mail it to [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic under discussion