Python 2′s print statement has been replaced by the
print() function, meaning that we have to wrap the object that we want to print in parentheses. Python 2 doesn’t have a problem with additional parentheses, but in contrast, Python 3 would raise a
SyntaxError if we called the print function the Python 2-way without the Parenthesis.
This change is particularly dangerous if you are porting code, or if you are executing Python 3 code in Python 2, since the change in integer-division behavior can often go unnoticed (it doesn’t raise a
SyntaxError).\ So, I still tend to use a
3/2.0 instead of a
3/2 in my Python 3 scripts to save the Python 2 guys some trouble (and vice versa, I recommend a
from __future__ import division in your Python 2 scripts).
Python 2 has ASCII
str() types, separate
unicode(), but no
byte type.Now, in Python 3, we finally have Unicode (utf-8)
strings, and 2 byte classes:
The usage of
xrange() is very popular in Python 2.x for creating an iterable object, e.g., in a for-loop or list/set-dictionary-comprehension.\ The behavior was quite similar to a generator (i.e., “lazy evaluation”), but here the xrange-iterable is not exhaustible – meaning, you could iterate over it infinitely.
Thanks to its “lazy-evaluation”, the advantage of the regular
range() is that
xrange() is generally faster if you have to iterate over it only once (e.g., in a for-loop). However, in contrast to 1-time iterations, it is not recommended if you repeat the iteration multiple times, since the generation happens every time from scratch!
In Python 3, the
range() was implemented like the
xrange() function so that a dedicated
xrange() function does not exist anymore (
xrange() raises a
NameError in Python 3).
Where Python 2 accepts both notations, the ‘old’ and the ‘new’ syntax, Python 3 chokes (and raises a
SyntaxError in turn) if we don’t enclose the exception argument in parentheses.