![]() |
VOOZH | about |
In this article, we will explore some important differences between Python 2.x and Python 3.x with the help of examples, focusing on the following libraries and modules.
Output in 2.x:
1
-2
Output in 3.x:
1.4
-1.4
Output in Python 2.x:
Hello, Geeks
This works tooOutput in Python 3.x:
Hello, Geeks
Output in Python 2.x:
<type 'str'>
<type 'str'>
Output in Python 3.x:
<class 'str'>
<class 'bytes'>
Python 2.x:
Python 3.x:
Output in Python 2.x:
1 2 3 4 1 2 3 4
Output in Python 3.x:
NameError: name 'xrange' is not defined
Output in Python 2.x:
name 'not_defined' is not defined Error Caused
Output in Python 3.x:
SyntaxError: invalid syntax
Correct way (works in both):
try:
x = not_definedexcept NameError as err:
print(err, 'Error Caused')
__future__ module allows Python 2 code to adopt Python 3 features early, making migration easier.
Example 1: Division behavior
Output (Python 2.x with future import):
1.4
-1.4
Output (Python 3.x):
1.4
-1.4
Example 2: print as a function
Output (Python 2.x with future import):
GeeksforGeeks
Output (Python 3.x with future import):
GeeksforGeeks