Table Of Contents

Previous topic

Object Oriented Programming

Next topic

Reading and writing files

This Page

A Quick Note about Python 3

There is currently a shift in the Python community as people move from Python 2.x to Python 3.x. The newer version of Python brings a number of improvements to the language, but also breaks backward compatibility. That is to say, programs written in Python 2.x will not necessarily run in Python 3.x. As a result of this, the Python community has been slow to move to the new version, as this requires that all of the pre-existing libraries and modules are updated. Nevertheless, this transition is now almost complete, with many of the large number of add-in packages now compatible with Python 3.x.

Since this transition is not totally complete, we will concentrate on Python 2.x in these workshops. You should be aware that this transition is taking place, and that there will be some changes if you move to Python 3.x. We introduce some of the many differences in the following subsections.

Integer division

In Python 2.x dividing two integers leads to unexpected results:

>>> 3 / 2
1

In Python 3.x this behavior has been fixed:

>>> 3 / 2
1.5

The print statement

In Python 3.x, the print statement has been replaced with the print() function. The new syntax is therefore:

>>> print("Hello World!")
Hello World!

instead of:

>>> print "Hello World!"
Hello World!

The former behaviour has been backported to Python 2.7, so in this tutorial, we always use the former to ease the transition to Python 3.x.

String formatting

Python 2 uses the following syntax for string formatting:

format_string % values

for example:

"%s %d" % ('spam', 1)

while Python 3 uses:

format_string.format(values)

and uses a {} notation instead of %, for example:

"{:s} {:d}".format('spam', 1)

The latter behaviour has been backported to Python 2.7, so in this tutorial, we always use the latter to ease the transition to Python 3.x.

Some lists are generators

In Python 2.x, the range function and keys attribute from dictionaries return lists:

>>> print(range(5))
[0, 1, 2, 3, 4]
>>> dict_example = dict(key1=5, key2='spam')
>>> print(dict_example.keys())
['key2', 'key1']

While in Python 3.x, we get:

>>> print(range(5))
range(0, 5)
>>> dict_example = dict(key1=5, key2='spam')
>>> print(dict_example.keys())
dict_keys(['key1', 'key2'])

Since recently, one can also index the new range class, but that is not possible with the dictionary keys. The latter also doesn’t make much sense, since the order of the keys is not fixed.

Writing future-proof scripts

Python 2.6 and 2.7 already support the new formatting notation and print function, so if you want to write future-proof scripts, you can already switch to using the new syntax in Python 2.

In addition, you can also start using the new division. To do this, simply do:

from __future__ import division

e.g. at the start of a script, and you can then use Python 3 syntax for division.

Copyright: Smithsonian Astrophysical Observatory under terms of CC Attribution 3.0 Creative Commons
 License