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 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:
print 'Python', python_version() >>> Python 2.7.6 print 'Hello World' >>> Hello World
print('Python', python_version()) >>> Python 3.4 print('Hello, World!') >>> Hello World Old: print x, New: print(x, ent=’ ‘)
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:
3 / 2 >>> 1 3 // 2 >>> 1
3 / 2 >>> 1.5 3 // 2 >>> 1
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
name = u'John' >>> type(name) <type 'unicode'>
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.
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:
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
#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:
try: call_function() except Exception, err: print err
try: call_function() except Exception as err: print err
range() function in Python 3 was implemented as Python 2
xrange() function was removed - calling
xrange() in Python 3 will raise a
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.
type(range(3)) >>> <type 'list'>
type(range(3)) >>> <class 'range'>
In case you really a need list instead of an iterable object, simply convert object to list using the
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
5 < '5' >>> True
5 < '5' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'int' and 'str'
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.