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:

print(‘Hello, world.’)

Or without:

print ‘Hello, world.’

In Python 3, parentheses must be used:

print ‘Hello, world.’

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

But in Python 3, a float value is returned:

print(5 / 3)
1.6666666666666667

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’Hello’)

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

print(‘Hello’)

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.

Want to start making money as a white hat hacker? Jump-start your white-hat hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from ethical hacking professionals.

Buy Now (96% off) >

Cover image by Cg__Prodigy/Pixabay



READ SOURCE

READ  17 tips and tricks to know when shopping at Dunelm - gotech daily

LEAVE A REPLY

Please enter your comment!
Please enter your name here