For Loops

One of the most powerful aspects of computer programs is their ability to perform repetion, which is the ability to do something many times. One of the main forms of repetition is the for loop, which does something a certain number of times.

Looping over a list

The most common use of for loops is to do the same thing to every item in a list. Python makes it easy to do this. The basic structure is simple

for item in list_of_items:
    block

where block is an indented list of Python commands. The first time through the loop the first object in list_of_items is assigned to item. The second time through the loop the second object in the list is assigned to item and so on until the end of the this.

For example, if we have a list of the nobel gases and want to print them out one at a time while mentioning that they are a nobel gas we would use a program like this.

In [1]:
nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']
for gas in nobel_gases:
    print "%s is a nobel gas." % gas
He is a nobel gas.
Ne is a nobel gas.
Ar is a nobel gas.
Kr is a nobel gas.
Xe is a nobel gas.
Rn is a nobel gas.

Looping over integers

This most common approach to using for loops across different programming languages is to loop over a set of integers, often using those integers as indexes for items in a list. We can do this in Python using the range() function.

Range

range() simple generates a list of integers when given starting and ending values. The list includes the starting value and goes up to, but does not include, the end value.

In [2]:
range(1, 10)
Out[2]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

If we leave out the start value then Python just assumes that it is equal to zero.

In [3]:
range(5)
Out[3]:
[0, 1, 2, 3, 4]

And we can also use integer steps that are greater than 1.

In [5]:
range(0, 10, 2)
Out[5]:
[0, 2, 4, 6, 8]

Looping with Range

Using range we can loop over integers/positions.

In [6]:
positions = range(10)
for i in positions:
    print i
0
1
2
3
4
5
6
7
8
9

Or, more simply

In [7]:
for i in range(10):
    print i
0
1
2
3
4
5
6
7
8
9

We could use this to rewrite our nobel gas program as follows:

In [8]:
nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']
for i in range(len(nobel_gases)):
    print "%s is a nobel gas." % nobel_gases[i]
He is a nobel gas.
Ne is a nobel gas.
Ar is a nobel gas.
Kr is a nobel gas.
Xe is a nobel gas.
Rn is a nobel gas.

Looping over more than one list

Looping over the indexes of a list is most useful if we have more than one list that we want to access at the same time. For example, if we also have a list of the atomic numbers of the nobel gases and we want to print out each gas along with it's atomic number we would modify our program as follows.

In [9]:
nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']
atomic_numbers = [2, 10, 18, 36, 54, 86]
for i in range(len(nobel_gases)):
    print "%s is a nobel gas with an atomic number of %s." % (nobel_gases[i], atomic_numbers[i])
He is a nobel gas with an atomic number of 2.
Ne is a nobel gas with an atomic number of 10.
Ar is a nobel gas with an atomic number of 18.
Kr is a nobel gas with an atomic number of 36.
Xe is a nobel gas with an atomic number of 54.
Rn is a nobel gas with an atomic number of 86.

Enumerate

This is such a common idiom in programming that Python provides an alternative approach that is often easier to read using the enumerate() function. This function returns something like a list that includes both the first value in a list and its position. For example,

In [11]:
nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']
for i, gas in enumerate(nobel_gases):
    print i, gas
0 He
1 Ne
2 Ar
3 Kr
4 Xe
5 Rn

This alows us to rewrite out atomic number program as

In [13]:
nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']
atomic_numbers = [2, 10, 18, 36, 54, 86]
for i, gas in enumerate(nobel_gases):
    print "%s is a nobel gas with an atomic number of %s." % (gas, atomic_numbers[i])
He is a nobel gas with an atomic number of 2.
Ne is a nobel gas with an atomic number of 10.
Ar is a nobel gas with an atomic number of 18.
Kr is a nobel gas with an atomic number of 36.
Xe is a nobel gas with an atomic number of 54.
Rn is a nobel gas with an atomic number of 86.

Which is arguably easier to read.