From the time Python was first released till now it has come with variant and advanced versions. Every version is better than earlier. In this section, we are going to review the Difference between Python 2 and Python 3.
For better understanding of Python 2 and Python 3 version have a look on these tables.
Basic Difference:
Python 2 | Python 3 |
Python 2 version was released in 2000. | Python 3 version was released in December 2008. |
It implemented technical details of the Python Enhancement Proposal (PEP). | The main purpose of this version release is to fix problems, exist in Python 2 version. |
Python 2 syntax is comparatively difficult to understand. | Python 3 syntax is simpler and easily understandable. |
In Python 2 version development process of code was easier than the versions released before. | To make the inversion process easy in Python 3, some features of Python 3 have been backported to Python 2.x versions. |
Basis of Comparison:
There are some basis of comparison, Python 2 and Python 3 are divided among these point.
Comparison is given below:
Basis of comparison | Python 2 | Python 3 |
Syntax | Syntax of Python 2 was difficult to understand. | Syntax is easily understandable and simple. |
Iteration | Range() is used for iterations in Python 2. | Range() function used to perform iterations. |
Function print | print “hello” | print (“hello”) |
Rules of ordering Comparisons | The rules, given for ordering comparison are very complicated. | The Rules of ordering comparisons are simplified. |
Exceptions | In this exceptions should be enclosed in notations. | In this exceptions should be enclosed in parenthesis. |
Division of Integers | It always gets an integer value, When two integers are divided. | Always get a float value, When two integers are divided. |
Backward compatibility | Python version 3 is not compatible with Python 2. | It is never reliable to port python 2 to python 3 version. |
Leak of variables | The value of the global variable will change while using it inside for-loop. | The value of variables never changes. |
Library | Many older libraries created for Python 2 is not forward-compatible. | Many recent developers are creating libraries which you can only use with Python 3 |
Unicode | To store Unicode string value, you require to define them with “u”. | In Python 3, default storing of strings is Unicode. |
Example Code:
Let’s view some example codes for better understanding of difference between Python 2 and Python 3.
Python 2 code | Python 3 code |
#python2 def main(): print “Python 2 version!” if name == “main”: main() | #python3 def main(): print(“Python 3 version!”) if name == “main”: main() |