What is the Difference Between Python2 and Python3

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”

Programmer's Academy
Programmer's Academy

But in Python3 the Print is made function that is used with the parentheses such as

Print (“hello world”)

Programmer's Academy
Programmer's Academy

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:

Programmer's Academy
Programmer's Academy

Python 3:

Programmer's Academy
Programmer's Academy

Difference in error handling

The syntax for showing an error message after a certain event is different in both versions of python.

Exception messagePython 2 syntaxPython 3 syntax
Sorry sir you cannot divide by 0raise 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:

Programmer's Academy
Programmer's Academy

Python 3:

Programmer's Academy
Programmer's Academy

Other Differences

DifferencesPython2Python 3
SyntaxComplex syntax not much user friendly and easy to understandSimple syntax, user friendly easy to understand
Backward compatibilityCompatible with python 3Not compatible with python 2
LibrariesOlder librariesAdvanced libraries as it is being used currently as mainstream python
LegacyIt is old version in future it will be soon vaguePython 3 is future and is here to stay
Release Date20002008

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.