Python 2 And Python 3 An in-depth comparison

Python 2 And Python 3: An in-depth comparison

Python 2 and Python 3 are not as similar as you might have imagined, but this post will help you understand the differences easily and it includes all the essential aspects of these versions of Python. Reading this article would make it easier to choose Python 2 or Python 3.

Introduction Python 2 And Python 3

Nowadays, in coding, Python is a popular programming language for beginners. As a powerful and highly versatile programming language, Python is used almost in every industry now and for many different coding projects.
Python’s journey from its invention in 1991 till now has been very long, and many versions of it have been released. Python 3 was released in 2008 and is currently in use. Python 3 was invented to overcome some of the shortcomings of Python 2, which was released in 2000.
Although Python 2 is almost outdated, it is still in use by some software professionals due to some of its unique features. Now let us understand the difference between Python 2 vs. Python 3.

Syntax Differences Between Python 2 and Python 3

Python 3 also introduced some changes to the syntax of the language. One example is the ‘print‘ statement. Other examples include the ‘range‘ and the ‘next‘ functions.

Print Example:

Python 2

print "Hello, World!"

In Python 2, this statement would print the string "Hello, World!" to the screen. In Python 3, this statement would cause an error.

Range Example:

Python 2

range(10)

In Python 2, this code would create a list of integers from 0 to 9. In Python 3, this code would produce an iterator from 0 to 9.

Iterator Example:

Python 2

next(iterator)

In Python 2, this code would return the next item in the ‘iterator‘ object. In Python 3, this code would cause an error.

Changes in String Handling

Python 2 stores its strings as “ASCII” by default. If strings are needed to be stored as “Unicode”, the string is to be marked with “u”; meanwhile, Python 3 stores its strings as Unicode by default mode.
The conversion of strings to Unicode is beneficial. Unicode strings are extremely versatile than ASCII strings because they can store letters from different languages, the standard Roman letters, numerals, and emoji. See an example below:

Python 2:

import sys

print 'We are using Python version', sys.version[:3]
print type('default string ')
print type(b'string with b ') #bytes and default both same in python 2
print type(u'string with u') #python2 supports Unicode as well

Output:

Output:
We are using Python version 2.7
<type 'str'>
<type 'str'>
<type 'unicode'>

Python 3:

import sys
print('We are using Python version', sys.version[:3])
print(type('default string ')) #unicode
print(type(b'string with b ')) #bytes and default both different in python 3

Output:

We are using Python version 3.8
<class 'str'>
<class 'bytes'>

Differences in Handling Integers and Division

While evaluating integer values in Python 2, the output doesn’t contain decimal values; it gives answers in round figures that create a problem further, whereas the output of Python 3 contains decimal values too, which is considered the exact output. Let’s understand this with the following example:

Python 2:

import sys

print 'We are using Python version', sys.version[:3]
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '5 / 4.0 =', 5 / 4.0
print '5 // 4.0 =', 5 // 4.0
—---------------------------------------

Output:

We are using Python version 2.7
3 / 2 = 1
3 // 2 = 1
5 / 4.0 = 1.25
5 // 4.0 = 1.0

Python 3:

import sys

print('We are using Python version', sys.version[:3])
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('5 / 4.0 =', 5 / 4.0)
print('5 // 4.0 =', 5 // 4.0)

Output:

We are using Python version 3.8
3 / 2 = 1.5
3 // 2 = 1.5
5 / 4.0 = 1.25
5 // 4.0 = 1.0

Note: One should be watchful while porting code from python 2 to 3 or vice versa about evaluating the integer value because no errors appear during compilation. Later, unexpected results may be faced as outputs.

Changes in Exception Handling

The syntax used to raise an exception is modified in Python 3. Look below at the syntax of both versions of python Below for raising exceptions.

Python 3:

raise IOError(“error message”)

The above syntax will work correctly in python 3. Now, look at the syntax below for Python 2.

Python 2:

raise IOError, “error message”

The above syntax will not work in Python 3; instead, the system will throw an error because the syntax is not closed within parentheses.

Changes in the Print Statement

Since in Python 2, print is a statement so it will be written as print<output content> ; whereas in Python 3 it works like a function, so here it is written as a print(<output content>) with the parentheses and the output inside the parentheses. Let’s understand this with the following syntax:

Python 2:

import sys

print 'We are using Python version', sys.version[:3]
print 'Hello, beautiful World!'
print('Hello, beautiful World!') # For parentheses to work, a space needs to be added after the print keyword
print "I love", ; print 'coding'
print "The answer is", 7*2
print 16, # Trailing comma is used to suppress the newline
print # A newline is printed
print >>sys.stderr, "fatal error"

Output:

We are using Python version 2.7
Hello, beautiful world!
Hello, beautiful world!
I love coding
The answer is 14
16
fatal error

Now look at the same print statement in Python 3 and notice the difference:

Python 3:

import sys

print('We are using Python version', sys.version[:3])
print('Hello, beautiful World!') #if parentheses are not used here it will throw a syntax error
print("I love",end="") ; print(coding')
print("The answer is", 7*2)
print(16,end=" ") #end = " " is used to append space instead of a new line
print() # A newline is printed
print("fatal error", file=sys.stderr)

Output:

We are using Python version 3.8
Hello, beautiful world!
I love coding
The answer is 14
16
fatal error

Note: In Python 2, a Print statement (“print()”) with parentheses will work, the reason is that here it is taken as a print statement followed by an (<expression>) in parentheses. On the other hand, in the case of Python, it will not work, and a syntax error will appear.

Conclusion 

With this, we have come to the end of this article, and we are sure that while studying it, we have provided every vital element to help you find out the differences between both languages better. This article should help you determine your choice, Python 2 vs. Python 3.

Python 3 is a better choice.

Leave a Comment

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