Python is almost 30 years old programming language. As an experienced old language, it has made things easy for its user as it has a library for everything. It has minimized user’s effort and provides built in functions for literally everything. The big difference between python and other languages is how easy it is to read. Initially python 2 was used as mainstream python language but it has some loose ends/gaps and to fill these gaps python 3 was introduced. Python 2.7 is still used widely as many systems are already running on it. In this article we will learn about difference between python 2 and python 3.
Print Function
In python 2 the Print was considered as a statement, for example you can print “hello world” using syntax
Print “hello world”
But in Python3 the Print is made function that is used with the parentheses such as
Print (“hello world”)
Integer division
In python 3 integer division was improved. In python 2 the resultant value after division is integer but in python the resultant value becomes float by default, hence giving more precise results. Let us check through an example.
Python 2:
Python 3:
Difference in error handling
The syntax for showing an error message after a certain event is different in both versions of python.
Exception message | Python 2 syntax | Python 3 syntax |
Sorry sir you cannot divide by 0 | raise IOError, “Sorry sir you cannot divide by 0” | raise IOError(“Sorry sir you cannot divide by 0”) |
Unicode
By default, python 3 sorts string as Unicode but in python 2 strings are stored as ASCII by default, to store Unicode string value we must mention “u” with string. Unicode is more flexible than ASCII, so it has wide options such as letters, characters, alphabets romans, symbols.
For-loop variable problem
In python 2 if the for-loop variable and global variable have a same name then during execution of program global variable value changes. This problem was solved in python 3.
Python 2:
Python 3:
Other Differences
Differences | Python2 | Python 3 |
Syntax | Complex syntax not much user friendly and easy to understand | Simple syntax, user friendly easy to understand |
Backward compatibility | Compatible with python 3 | Not compatible with python 2 |
Libraries | Older libraries | Advanced libraries as it is being used currently as mainstream python |
Legacy | It is old version in future it will be soon vague | Python 3 is future and is here to stay |
Release Date | 2000 | 2008 |