# for loops¶

## Sources¶

This lesson is based on the Software Carpentry group’s lessons on Programming with Python.

## Basics of for loops¶

Loops allow parts of code to be repeated some number of times.

Let’s consider an example. Suppose we want to take a word and print out each letter of the word separately. We could do the following:

[11]:

word = 'snow'

[12]:

print(word[0])

s

[13]:

print(word[1])

n

[14]:

print(word[2])

o

[15]:

print(word[3])

w


But this is a bad idea. Why? Well there are two reasons. First, it does not scale nicely for long strings, and will take forever to type in. Second, it won’t work if the word is not 4 characters long.

[16]:

word = 'ice'

[17]:

print(word[0])

i

[18]:

print(word[1])

c

[19]:

print(word[2])

e

[20]:

print(word[3])

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-929d2b055d22> in <module>()
----> 1 print(word[3])

IndexError: string index out of range


### Introducing the for loop¶

We could do a much better job by using a for loop.

[21]:

word = 'snow'

[22]:

for char in word:
print(char)

s
n
o
w


Not only is this shorter, but it is also more flexible. Try out a different word such as freezing fog. Still works, right?

[ ]:




### for loop format¶

for loops in Python have the general form below.

for variable in collection:
do things with variable


The variable can be any name you like, and the statement of the for loop must end with a :. The code that should be executed as part of the loop must be indented beneath the for loop statement, and the typical indentation is 4 spaces. There is no additional special word needed to end the loop, you simply change the indentation back to normal. for loops are useful to repeat some part of the code a definite number of times.

Like many other programming concepts, the idea of looping through actions is something that is already perhaps more familiar to you than you think. Consider your actions during a given day. Many people have certain routines they follow each day, such as waking up, taking a shower, eating breakfast and brushing their teeth. In Python code, we might represent such actions as follows:

for day in my_life:
wake_up()
take_shower()
eat_breakfast()
brush_teeth()
...


Note that my_life would be a list of the days of your life, and the actions you take are represented as functions, such as wake_up(). Furthermore, by following this kind of list of repeating actions we’re able to start the day effectively even before the first cup of coffee :).

### Another for loop example¶

Let’s consider another example.

[23]:

length = 0
for letter in 'blizzard':
length = length + 1

[24]:

print('There are', length, 'letters')

There are 8 letters


Can you follow what happens in this loop?

### for loop variables¶

Note that the variable used in the loop, letter in the case above is just a normal variable and still exists after the loop has completed with the final value given to letter.

[25]:

letter = 'x'
for letter in 'sleet':
print(letter)

s
l
e
e
t

[26]:

print('After the loop, letter is', letter)

After the loop, letter is t


### for loops and lists¶

A loop can be used to iterate over any list of values in Python. So far we have considered only character strings, but we could also write a loop that performs a calculation a specified number of times.

[27]:

for value in range(5):
print(value)

0
1
2
3
4


What happens here? Well, in this case, we use a special function called range() to give us a list of 5 numbers [0, 1, 2, 3, 4] and then print each number in the list to the screen. When given a integer (whole number) as an argument, range() will produce a list of numbers with a length equal to the specified number. The list starts at 0 and ends with number - 1. You can learn a bit more about range by typing

[28]:

help(range)

Help on class range in module builtins:

class range(object)
|  range(stop) -> range object
|  range(start, stop[, step]) -> range object
|
|  Return an object that produces a sequence of integers from start (inclusive)
|  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
|  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
|  These are exactly the valid indices for a list of 4 elements.
|  When step is given, it specifies the increment (or decrement).
|
|  Methods defined here:
|
|  __bool__(self, /)
|      self != 0
|
|  __contains__(self, key, /)
|      Return key in self.
|
|  __eq__(self, value, /)
|      Return self==value.
|
|  __ge__(self, value, /)
|      Return self>=value.
|
|  __getattribute__(self, name, /)
|      Return getattr(self, name).
|
|  __getitem__(self, key, /)
|      Return self[key].
|
|  __gt__(self, value, /)
|      Return self>value.
|
|  __hash__(self, /)
|      Return hash(self).
|
|  __iter__(self, /)
|      Implement iter(self).
|
|  __le__(self, value, /)
|      Return self<=value.
|
|  __len__(self, /)
|      Return len(self).
|
|  __lt__(self, value, /)
|      Return self<value.
|
|  __ne__(self, value, /)
|      Return self!=value.
|
|  __new__(*args, **kwargs) from builtins.type
|      Create and return a new object.  See help(type) for accurate signature.
|
|  __reduce__(...)
|      helper for pickle
|
|  __repr__(self, /)
|      Return repr(self).
|
|  __reversed__(...)
|      Return a reverse iterator.
|
|  count(...)
|      rangeobject.count(value) -> integer -- return number of occurrences of value
|
|  index(...)
|      rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.
|      Raise ValueError if the value is not present.
|
|  ----------------------------------------------------------------------
|  Data descriptors defined here:
|
|  start
|
|  step
|
|  stop



#### Poll question pause¶

The program below will print numbers to the screen using the range() function.

for i in range(...):
print(i)


Using the documentation that is produced when you run help(range), what values would you replace the ... in the parentheses of the range() function with to have the following output printed to the screen?

2
5
8


### Calculating values in for loops¶

Often when you use for loops, you are looping over the values in a list and either calculating a new value or modifying the existing values. Let’s consider an example.

[29]:

mylist = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]

[30]:

print(mylist)

[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]

[31]:

for i in range(6):
mylist[i] = mylist[i] + i
print(mylist)

[0.0, 2.0, 4.0, 6.0, 8.0, 10.0]


So, what happened? We first create a list of 6 numbers. Then, we loop over 6 values using the range() function and add each value to the existing location in mylist. What would happen if we ran this for loop a second time?

### Looping over the length of lists¶

One of the drawbacks in the example above is that we need to know the length of the list before running that for loop example. However, we already know how to find the length of a list using the len() function, and we can take advantage of this knowledge to make our for loop more flexible.

[32]:

for i in range(len(mylist)):
mylist[i] = mylist[i] + i
print(mylist)

[0.0, 3.0, 6.0, 9.0, 12.0, 15.0]


We’ve done exactly what we had done in the previous example, but replaced the known length of the list 6 with use of the len() function to provide the list length. Now if we add or remove values in mylist, our code will still work as expected.

[33]:

mylist.append(18.0)

[34]:

mylist.append(21.0)

[35]:

print(mylist)

[0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0]

[36]:

for i in range(len(mylist)):
mylist[i] = mylist[i] + i
print(mylist)

[0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 24.0, 28.0]


Using the len() function with range() to perform calcluations using list or array values is an extremely common operation in Python.

#### Poll question pause¶

What output would the following program produce?

word = 'ice pellets'
for i in range(len(word)):
print(word[i])