What’s new in Python 3
At the time of writing Python 2 is still in use, but it's a good idea to upgrade to Python 3 or make your project available for both versions. January 1, 2020, is The End Of Life Date for Python 2.7, meaning it will not be maintained after that.
Python 3 is different from the previous Python releases as it is not compatible with older versions. Making Python 3 backward incompatible was needed to implement a variety of great features that we will talk about in this article.
Let's start with the simplest change.
The Print
function
The old print statement has been replaced with a function print()
. Calling print as a statement in Python 3 throws a SyntaxError
. All features of the print statement are covered by print function so there is no loss of functionality. Example:
Python 2
print 'Python', python_version()
>>> Python 2.7.6
print 'Hello World'
>>> Hello World
Python 3
print('Python', python_version())
>>> Python 3.4
print('Hello, World!')
>>> Hello World
Old: print x,
New: print(x, ent=’ ‘)
Integers
In Python 2 there were two kinds of integers: short integers (32-64bits) and long integers (limited by available memory). Python 3 unifies Long Integers and Integers into one type Integer. In Python 2 /
is an integer division, in Python 3 /
results in a float and //
is used for integer division. Example:
Python 2
3 / 2
>>> 1
3 // 2
>>> 1
Python 3
3 / 2
>>> 1.5
3 // 2
>>> 1
Unicode
Python 2 and 3 have two types of strings, byte sequences (ASCII characters) and Unicode strings. The Unicode string represents a series of characters that are represented as a number (0 - 1,114,111). In Python 2 you had to add u
in front of the string to make it Unicode. Python 3 makes this more accessible by making all strings Unicode by default. If you want a byte sequence instead of the Unicode string you need to mark the string with a b
. Example:
Python 2
name = u'John'
>>> type(name)
<type 'unicode'>
Python 3
name = 'John'
>>> type(name)
<class 'str'>
Following the Python 3 documentation, the most important tip for creating Unicode aware programs is:
Software should only work with Unicode strings internally, decoding the input data as soon as possible and encoding the output only at the end.
Exceptions
There should be one -- and preferably only one -- obvious way to do it
Python 2 raise statement violates that principle with accepting raising exceptions with and without parentheses. In Python 3 you will get a SyntaxError
if the exception arguments are not enclosed in parentheses. Example:
Python 2
raise IOError, 'error in file'
>>> File "<stdin>", line 1, in <module>
IOError: error in file
#or
raise IOError('error in file')
>>> File "<stdin>", line 1, in <module>
IOError: error in file
Python 3
#wrong
raise IOError, 'error in file'
>>> SyntaxError: invalid syntax
#right
raise IOError('error in file')
>>> File "<stdin>", line 1, in <module>
OSError: error in file
The handling of exceptions has slightly changed in Python 3. You must now use an as
keyword. Example:
Python 2
try:
call_function()
except Exception, err:
print err
Python 3
try:
call_function()
except Exception as err:
print err
xrange
The range()
function in Python 3 was implemented as Python 2 xrange()
. The xrange()
function was removed - calling xrange()
in Python 3 will raise a NameError
.
Returning iterable objects
Iterators have better memory consumption than the lists, that's why in Python 3 the return values of some functions are changed.
Instead of returning lists, these functions now return iterable objects:
map()
returns an iterator map object.filter()
- returns an iterator filter object.zip()
- returns an iterator zip object.range()
- returns an iterator range object.
Example:
Python 2
type(range(3))
>>> <type 'list'>
Python 3
type(range(3))
>>> <class 'range'>
In case you really a need list instead of an iterable object, simply convert object to list using the list()
function.
Ordering Comparisons
Comparing unorderable types raises a TypeError
in Python 3. As an example in Python 2 you can compare string and integer objects, but in Python 3 it fails with the a TypeError
. Example:
Python 2
5 < '5'
>>> True
Python 3
5 < '5'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
Conclusion
There are more changes and new features in Python 3 which you should check out.
As most of the top libraries are now Python 3 compatible (around 344 of 360 most popular packages), it's safe to pick Python 3 for most of your new projects and consider migrating your existing codebase over from Python 2 to Python 3.