Databases Reference
In-Depth Information
Python 2.7, the most recent minor release in the Python series, will also be the
last
2.X
release. Although it will be updated with bug fixes for an extended period of time, new
Python code development is now carried out exclusively on the 3.X line. The NumPy
package, h5py, PyTables, and many other packages now support Python 3. While (in
my opinion) it's a little early to recommend that newcomers start with Python 3, the
future is clear.
So at the moment, there are two major versions of Python widely deployed simultane‐
ously. Since most people in the Python community are used to Python 2, the examples
in this topic are also written for Python 2. For the most part, the differences are minor;
for example, on Python 3, the syntax for
print
is
print(foo)
, instead of
print foo
.
Wherever incompatibilities are likely to occur (mainly with string types and certain
dictionary-style methods), these will be noted in the text.
“Porting” code to Python 3 isn't actually that hard; after all, it's still Python. Some of the
most valuable features in Python 3 are already present in Python 2.7. A free tool is also
available (
2to3
) that can accomplish most of the mechanical changes, for example
changing
print
statements to
print()
function calls. Check out the migration guide
Code Examples
To start with, most of the code examples will follow this form:
>>>
a
=
1.0
>>>
print
a
1.0
Or, since Python prints objects by default, an even simpler form:
>>>
a
1.0
Lines starting with
>>>
represent input to Python (
>>>
being the default Python prompt);
other lines represent output. Some of the longer examples, where the program output
is not shown, omit the
>>>
in the interest of clarity.
Examples intended to be run from the command line will start with the Unix-style "
$
"
prompt:
$ python --version
Python 2.7.3
Finally, to avoid cluttering up the examples, most of the code snippets you'll find here
will assume that the following packages have been imported:
>>>
import
numpy
as
np
# Provides array object and data type objects
>>>
import
h5py
# Provides access to HDF5