Python2 vs. Python3

py2vpy3

Guido van Rossum created Python in 1991, he decided to clean up Python 2.x properly, without considering any newer releases to it as well as backward compatibility. The whole Python community was pretty much skeptical when they received Python 3.x. Most of the things written in Python 2.x were not compatible with Python 3.x, as it did not support backward compatibility. Many applications and frameworks needed to rewritten completely due to this, hence, it was very difficult to port to Python3 from Python2. Why was it that difficult? What are the differences?

Let us see some of the key differences between Python 2 and Python 3.

Print

In Python2, print is treated as a statement rather than a function. You don’t need wrap the text in parentheses. But, it was very confusing, as most the actions or work that you do with Python requires arguments to be placed inside the parentheses .In Python3, “print” works like a function, where you wrap the text inside the parentheses. If you don’t use parentheses, there will be a syntax error. Though, many Python2 programmers find this change to be irritating, but it really helps in preventing mistakes as some time if you put parentheses around a comma-separated lists then you would get unexpected outcomes. You can see this difference of print function in the example.

In Python2,

>>> print "Hello World!"
Hello World

In Python3,

>>> print("Hello World!")
Hello World!

In Python3, syntax error, when executed without parentheses,

>>> print "Hello World!"
SyntaxError: Missing parentheses in call to 'print'

Integer Division

In Python2, whenever you type any number without any digit after decimal, it treats that number as integer. This might lead to some unexpected  results during division. We will see it with an example.

Python2,

>>> 3/2
1

Here, when we made the division of 3 by 2, we got 1, this is because Python2, assumes that as the programmer entered integer values, the output should be also integer and hence it rounds off the output to the nearest value. If you want, the output as 1.5 then you will have to entered the input in float as shown here.

>>> 3.0/2.0
1.5

Thus, in Python2, if you want the output in float then you need to have the input also in float.

Below is the output from Python3, you can see that even though the input is in integer, you get the output in float which very much intuitive for new programmers.

>>> 3/2
1.5

List Comprehension-Loop Variables

In Python2, when a variable that is being iterated over a for-loop and a global variable, both are having same name, the value of the global variable changes whenever there is an iteration, this is highly undesirable. I have shown it here in the example that follows.

Python2

>>> i=1
>>> print "before i=",i
before i= 1
>>> print "Jekishan", [i for i in range(5)]
Jekishan [0, 1, 2, 3, 4]
>>> print "after i=",i
after i= 4

Python3

>>> i=1
>>> print ("before i=",i)
before i= 1
>>> print ("Jekishan",[i for i in range(5)])
Jekishan [0, 1, 2, 3, 4]
>>> print("after i=",i)
after i= 1

Hence, in Python 3.x for-loop variables don’t leak into the global namespace anymore!

Unicode Strings

In Python2, if you want to store a string as Unicode, you need to mark it with “u”, whereas in Python3, all the strings are stored in Unicode by default. Unicode strings are much more versatile compared to ASCII, which is the default standard for Python2. You can store emojis, letters from foreign languages, roman letters as well as numerals in Unicode.

Raising Exceptions

Syntax for raising an error in Python2 and Python3, are different.

In Python2, to raise an IO Error, you would write

>>>raise IOError, "This is an IO Error"

In Python3, to raise an IO Error, you will write

>>>raise IOError ("This is an IO Error")

This example, again reminds you that in Python3, you need to pass arguments in the parentheses, which creates less confusion.

Apart from this, there are many other examples of slight differences, in the syntax of Python2 and Python3. Python3.5 works approximately at the same speed as 2.7, though there are some benchmarks which suggests that Python3 is faster compared to Python2.

 

In next post, I will write about which version of Python to use where?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s