This section will give a very brief introduction to the Python language. For more information on the Python language, go to the Python homepage.

Executing Python code

You can execute Python code interactively by starting the interpreter like this:

[hugo@cpu1 hugo]$ python
Python 2.2.1 (#1, Aug 30 2002, 12:15:30)
[GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello'

You can also put the print 'hello' line in a file ( and execute it as a Python script:

[hugo@cpu1 hugo]$ python

Or like this:

[hugo@cpu1 hugo]$ python -i
>>> print 'hi!'

Finally, you can put #!/usr/bin/env python in the first line of the file, make it executable (chmod +x and execute it like any other executable.


Python has the following predefined types:

type description example
bool boolean False
int integer 117
float floating point number 1.78
complex complex number 0.5 + 2.0j
str string 'abc'
tuple tuple (1, 'hmm', 2.0)
list list [1, 'hmm', 2.0]
dict dictionary {'a': 7.0, 23: True}
file file file('stuff.dat', 'w')

A dict object is mapping from keys to values:

>>> d = {'s': 0, 'p': 1}
>>> d['d'] = 2
>>> d
{'p': 1, 's': 0, 'd': 2}
>>> d['p']

A list object is an ordered collection of arbitrary objects:

>>> l = [1, ('gg', 7), 'hmm']
>>> l[1]
('gg', 7)
>>> l.append(1.2)
>>> l[-2]

A tuple behaves like a list - except that it can't be modified inplace. Objects of types list and dict are mutable - all the other types listed in the table are immutable, which means that once an object has been created, it can not change.


List and dictionary objects can change. Variables in Python are references to objects. This is demonstrated here:

>>> a = ['q', 'w']
>>> b = a
>>> a.append('e')
>>> a
['q', 'w', 'e']
>>> b
['q', 'w', 'e']

Numeric Python

ASE makes heavy use of an extension to Python called Numeric Python. The Numeric module defines an array type that can hold large arrays of uniform multidimensional numeric data. An array is similar to a list or a tuple, but it is a lot more powerful and efficient.

>>> import Numeric as num
>>> a = num.zeros((3, 2), num.Float)
>>> a[[, 1]] = 1
>>> a[1] = 2
>>> a
array([[ 0.,  1.],
       [ 2.,  2.],
       [ 0.,  1.]])
>>> a.shape
(3, 2)


A loop in Python can be done like this:

>>> things = ['a', 7]
>>> for x in things:
...     print x

The things object could be any sequence. Strings, tuples, lists, dictionaries, Numeric arrays and files are sequences.

Functions and classes

A function is defined like this:

>>> def f(x, y):
...     return x + 2 * x * y
>>> f(1, 2)

A class is defined like this:

>>> class C:
...     def __init__(self, x):
...         self.x = x
...     def M(self, y):
...         return f(self.x, y)

The __init__() function is called a constructor. You can think of a class as a template for creating user defined objects:

>>> o = C(1)
>>> o.M(2)

Here we just called the method M of the object o (o is an instance of the class C).

Importing modules

If you put the definitions of the function f and the class C in a file, then you can use that code from another piece of code:

from stuff import f, C
print f(1, 2)
print C(1).M(2)


import stuff
print stuff.f(1, 2)
print stuff.C(1).M(2)


import stuff as st
print st.f(1, 2)
print st.C(1).M(2)