How To: Python 2 vs. Python 3 — Important Differences Every Hacker Should Know

Python 2 vs. Python 3 — Important Differences Every Hacker Should Know

Python is commonly touted as one of the best programming languages for beginners to learn, and its straightforward syntax and functionality makes that hard to argue with. But a lot of tutorials still use Python 2, which is outdated now. Python 3 introduces many new features, and it's important to be aware of them going forward, as well as the key differences between Python 3 and its predecessor.

Python 2 was first released in 2000. It improved upon earlier versions of the language and introduced features common to other programming languages such as garbage collection, list comprehension, and Unicode support. Version 2.1 saw a few minor upgrades, and 2.2 was released at the tail end of 2001. Version 2.2 featured type unification, which merged types and classes into a single hierarchy, transforming Python into an object-oriented language.

Versions progressed until Python 2.7, which was to be the last major release until Python 3. Python 2 was officially retired on the first day of 2020. Python 3, first started in 2008, is now the most current option, with version 3.8.3 being the latest update.

Print Function

The first and possibly the most noticeable difference in Python 3 is the print function. In Python 2, print could be used with parentheses with either single or double quotation marks:

print('Hello, Null Byte.')
Hello, Null Byte.

or

print("Hello, Null Byte.")
Hello, Null Byte.

Or without, using just a space between it and what needs printed:

print 'Hello, Null Byte.'
Hello, Null Byte.

or

print "Hello, Null Byte."
Hello, Null Byte.

In Python 3, parentheses must be used:

print('Hello, Null Byte.')
Hello, Null Byte.

or

print("Hello, Null Byte.")
Hello, Null Byte.

So Python 2 is more versatile in this case, but Python 3 makes the code more concise with fewer ways to do the same thing, and that can actually be easier to learn and use.

Integer Division

Python 3 also handles the division of integers differently. In version 2, dividing two integers would yield an integer as a result:

print 5 / 3
1

or

print 5/3
1

or

print(5 / 3)
1

or

print(5/3)
1

To get the full float value with decimals, you would need to use decimals for the print function, like so:

print 5.0 / 3.0
1.6666666666666667

or

print 5.0/3.0
1.6666666666666667

or

print(5.0 / 3.0)
1.6666666666666667

or

print(5.0/3.0)
1.6666666666666667

But in Python 3, a float value is returned no matter if you use decimals or just integers (though, you do need to use parentheses):

print(5 / 3)
1.6666666666666667

or

print(5/3)
1.6666666666666667

or

print(5.0 / 3.0)
1.6666666666666667

or

print(5.0/3.0)
1.6666666666666667

Python 3 is more how a calculator would work, which is a lot easier to understand and remember for people just getting into Python.

Range Function

In Python 2, the xrange function is used for iteration, in for loops, as well as iterating through a list or dictionary:

for x in xrange(1, 10):
    print(x)

In version 3, xrange is replaced by the range function:

for x in range(1, 10):
    print(x)

The advantage of xrange is that it is faster when iterating over a list one time. However, it doesn't support splicing and other list-oriented methods, so using range is now preferred.

Unicode

Python 2 implicitly stores strings as ASCII values. To store them using Unicode, it needs to be specified with u like so:

print(u'This is Unicode')
This is Unicode

or

print(u"This is Unicode")
This is Unicode

vs.

print('This is NOT Unicode but ASCII')
This is NOT Unicode but ASCII

or

print("This is NOT Unicode but ASCII")
This is NOT Unicode but ASCII

In Python 3, strings are now treated as Unicode by default, so there is no need to specify:

print(u'This is Unicode')
This is Unicode

or

print(u"This is Unicode")
This is Unicode

vs.

print('This is ALSO Unicode')
This is ALSO Unicode

or

print("This is ALSO Unicode")
This is ALSO Unicode

Exceptions

The way exceptions and errors are handled has changed in Python 3. In version 2, exceptions look something like this:

try:
    checking_error
except NameError, err:
    print err, ‘Error found’

In version 3, we need to add the as keyword:

try:
    checking_error
except NameError as err:
    print(err, ‘Error found’)

Python 3 also handles the way exceptions are raised. In version 2:

raise IOError, ‘error’

In version 3, parentheses are required:

raise IOError(‘error’)

Variable Leakage

In Python 2, variables in a for loop will leak into the global namespace. This means global variables can change while inside the loop. In Python 3, it was tweaked, and the value of the variables never changes.

Future Module

Python 3 added certain keywords and features that aren't compatible with Python 2. Luckily, there is an easy way to use the functionality of version 3 in code running Python 2 — the _future_ module. To help migrate to Python 3, simply use future imports:

from _future_ import division

Now there is Python 3 support in Python 2 code.

Advantages of Python 3

Now that we've gone over the changes in Python 3, it's easy to see some of the advantages it has over the retired version. Things like the print function actually behaving like a function are nice, as well as the way division is handled. Unicode support is another advantage 3 has over 2 — no more having to worry about specifying it. Error handling is also more robust in Python 3 and allows for easier tracking and debugging.

One of the main goals of Python 3 was to remove some of the redundancy that was present in version 2. Since this language is often recommended to and used by beginners, the developers wanted to make it clear there was only one way to perform a certain action. Lastly, the biggest advantage of Python 3 is that it is being actively developed, allowing the codebase to become more efficient and functional as time goes on.

Python 2 is officially retired, so join the party and migrate to Python 3 as soon as possible. And those of you with original Python apps on GitHub, make sure to update to Python 3 so your users won't run into any hiccups.

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

Cover image by Cg__Prodigy/Pixabay

Be the First to Comment

Share Your Thoughts

  • Hot
  • Latest