A Few Minor Differences Between Python 2 and Python 3

0
3992

It’s almost 11 years since Python 3 (Py3K) was released in December 2008. The earliest version of Python was released in 1991 and since then, a few fundamental flaws had developed in the language. Python 3 was developed with the guiding principle of “Reducing feature duplication by removing old ways of doing things.” Sadly, Python 3 is not backward compatible.

Figure 1: Differences between print in Python 2 and Python 3

I had recently written a series of articles on Python for a children’s science magazine. Though my programming experience is limited to Python 2, I decided to stick to Python 3 because the target audience is young. Python 3 is the future of this language. I came across a number of subtle differences between Python 2 and Python 3 while writing that series of articles. Well, this was to be expected as Python 3 is not backward compatible with Python 2. But still, I believe it will be useful to identify and list out such minor differences between Python 2 and Python 3 so that a programmer can easily migrate from the former to the latter.

Rather than dealing with the philosophical aspects of migrating from Python 2 to Python 3, let’s consider practical situations in which a Python 2 programmer should exercise caution while programming in Python 3.

Differences with print
In Python 2, print is a statement whereas in Python 3, it is a function. Parentheses are not necessary in the Python 2 print statement whereas in Python 3, it is necessary to do so. For example, the Python 2 print statement print “Hello World” should be written as print (“Hello World”) in Python 3. Another difference with print in both versions occurs when a print statement should not move the cursor to a new line. Consider the small Python script py1.py shown below. Please note the comma after the first print statement.

print(“Open Source “),
print(“For You”)

Figure 1 shows the execution of the script in Python 2 and Python 3, using the commands python2 and python3, respectively. Python 2 prints the two strings in a single line whereas Python 3 prints the two strings in two lines. From the figure we can also see that the command python in Linux still translates to the command python2 which interprets the script as Python 2.
In order to print a string without moving the cursor to a new line in Python 3, we can use the print function as shown in the Python script py2.py given below.

print(“Open Source “, end=””)
print(“For You”)

Figure 2 shows that in Python 3, the two strings are printed in a single line, whereas in Python 2 the script py2.py results in a syntax error. This is yet another difference while using print in Python 2 and Python 3.

The division operator
The division operator ‘/’ results in integer division in Python 2, whereas in Python 3, the division operator performs normal division. For example, when the operation ‘5/2’ is performed in Python 3 we get 2.5 as the result. But in Python 2, we get 2 as our result. Figure 3 shows this difference when executed on Python 3 and Python 2 shells. Figure 3 also shows us that the operations ‘5.0/2.0’, ‘5.0/2’, and ‘5/2.0’ will result in 2.5 as the output in Python 2 also.

ASCII strings in Python 2
In Python 3, strings are stored as Unicode by default, whereas in Python 2, we need to prefix a string with a ‘u’ to make it a Unicode string. In Python 2, strings are stored as ASCII, by default. This support for Unicode by Python 3 is really important because Unicode supports a far richer character set when compared with ASCII.

Figure 2: No new line print in Python 3
Figure 3: Differences in the division operator

Functions that are not supported by Python 3
The function xrange( ) supported by Python 2 is not supported in Python 3. The range( ) function in Python 3 behaves similar to the xrange( ) function in Python 2. Similarly, the function raw_input( ) supported by Python 2 is not supported by Python 3. In Python 3, we have to use the function input( ). Remember the use of these deprecated functions in Python 3 will lead to an error.

The differences pointed out here are just a series of one-liners and do not represent all the differences between the two versions, but if somebody could build on from this article, then all the differences between Python 2 and Python 3 can be fully enumerated to benefit the Python programming community. But even if you are planning to stick with Python 2, you don’t need to worry much. 2to3 is a Python program that reads Python 2 source code and converts it into Python 3 source code. A standard library called lib2to3, which contains a lot of fixes, is used to do this conversion.

The lib2to3 library is flexible and generic, thus allowing us to write our own fixes for 2to3 so that the unknown differences can also be handled. Thus, a complete list of differences between Python 2 and Python 3 comes very handy in this situation. The URL https://docs.python.org/2/library/2to3.html will take us to the documentation of 2to3. This page also mentions many other differences between Python 2 and Python 3 not discussed in this article. There are some online converters also — for converting a Python 2 script to Python 3. But a programmer should be very careful while using any such tool. Consider the Python 2 script called py3.py.

num = 5/2
print num
Figure 4: Conversion from Python 2 to Python 3

On execution, the Python 2 script py3.py prints 2 as the output. Figure 4 shows the use of 2to3 to convert the Python 2 script py3.py to Python 3. The command 2to3 -w py3.py converts the Python 2 script py3.py to Python 3. From the figure, it is clear that the print statement of Python 2 is properly converted to a print function whereas the integer division operator ‘/’ is left unchanged. Figure 4 also shows the output of the converted Python 3 script is different from the output of the original Python 2 script. The Python 3 script prints 2.5 as output. This is not at all acceptable. The output of the script should not change when converted from Python 2 to Python 3. Thus the blind use of a tool to convert a Python 2 script to Python 3 is not at all advisable because it might lead to potential bugs in the Python 3 script. This is why a judicious use of such conversion tools and an in-depth knowledge of the differences between Python 2 and Python 3 are necessary. An expanded list like the one we have discussed here will be very useful in such situations.

LEAVE A REPLY

Please enter your comment!
Please enter your name here