Pythongasm - Major Differences Between Python 2 and Python 3

Copied to clipboard

Major Differences Between Python 2 and Python 3

Jan 01, 2020 • 5 minutes • 81 views

Introduction

While Python 3 was released in 2009, many developers are still running version 2 — 10 years later. Instagram was running Python 2.7 until recent years (moved to Python 3 in 2017).

1 January 2020 marked the end of life for Python 2, and it has officially retired according to the Python Clock.

python-clock

This article isn't aimed at Python 2.7 developers who are looking for "Python 3.x syntax", or at least that's not the primary reason for writing this article. Instead, we will have a look at the problems in Python 2, and how they were addressed in the newer versions.

Let us see some major changes that took place with Python 3.

Character Encoding

The default character encoding for Python 2 was ascii.

While ASCII contains the most frequently used characters (Latin, punctuation, etc.), it lacks the 100,000+ characters that are available in the Unicode character set. In fact, Unicode is a superset of ASCII with its first 128 characters being the same as ASCII characters.

It doesn't mean there was no support for Unicode characters, it's just that it was a bit messed up.

Data types

Byte string (type str) and Unicode string (type unicode) in Python 2.x would combine if the byte string had only ASCII characters (instead of throwing TypeError).

We can't really blame older Python versions for this. The wide use of most of the Unicode characters (like non-Latin characters and modern-day emojis) was not foreseen. In fact, until 2010, 50% of the web was still not using utf-8 (Unicode encoding).

In Python 3, all the text is Unicode — type str is a Unicode object.

TL;DR: The default encoding is now utf-8, and "I ♥️ Python" is str in Python 3.

For more details on this topic, you can have a look at this presentation by Ned Batchelder (PyCon 2012)

The Print Function

This is probably the most popular update in Python 3. The print statement no longer exists, and we now have this print() function:

print(*objects
    , sep=' '
    , end='\n'
    , file=sys.stdout
    , flush=False)

Usage:

print("Hello", end=‘\t')
print("World"
Hello   World

String Formatting

With the release of version 3.6, string formatting was improved with what we call f-strings.

name = "Joe"
message = f"Hello, {name}!"

f-strings, also called ‘format string literals' enhance the readability of code. However, the traditional methods of string formatting have not been deprecated.

Variable Leakage

x = 256
wholeNumbers = [x for x in range(10)]
print(x)

What do you think should be the value of x? It should be 256, right? That's because the x that we have used in the list comprehension was just a local variable and it isn't meant to affect the code outside. Turns out it does, in Python 2.

Python 2 'leaks' out the variable, making x's value equal to 9. This issue has been fixed in Python 3, and you will now get 256 as the output.

The Walrus Operator

It would be better to call it a Python 3.8 change. The walrus operator is an "informal name" for Assignment Expressions.

Walrus

According to the official PEP index, this operator is:

a way to assign to variables within an expression using the notation NAME := expr.

Suppose we have this block of code:

while True:
    name = input("ENTER NAME: ")
    if(name=='exit()'):
        break
    else:
        print(f'Hello {name}!')

This can be reduced to:

while (name := input("ENTER YOUR NAME: ")) != 'exit()':
    print(f'Hello {name}!')

The variable name is assigned the value from input() function on the spot. The aim of assignment expressions is to improve the readability of code.

Conclusion

Apart from these five major updates, there were many other changes we saw in Python 3. For example, we now need to use Exception as e: instead of Exception, e: in error catching expressions.

You can also use this tool called 2to3 to update your code to Python 3 like this:

2to3 old-code.py

Hope you enjoyed reading this article.

Further Readings:


Permanent link to this article Read Next
pygs.me/002 Python One-Liners You Should Know

Comments

styling code quotes links