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.

  1. 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:

    In [1]: word = 'snow'
    
    In [2]: print(word[0])
    s
    
    In [3]: print(word[1])
    n
    
    In [4]: print(word[2])
    o
    
    In [5]: 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.

    In [6]: word = 'ice'
    
    In [7]: print(word[0])
    i
    
    In [8]: print(word[1])
    c
    
    In [9]: print(word[2])
    e
    
    In [10]: print(word[3])
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-10-e3303df6f566> in <module>()
    ----> 1 print(word[3])
    
    IndexError: string index out of range
    
  2. We could do a much better job by using a for loop.

    In [11]: word = 'snow'
    
    In [12]: for char in word:
       ....:     print(char)
       ....: 
    s
    n
    o
    w
    

    Note here that the ... is displayed in the IPython console when entering code in a loop and you do not need to type in the .... Not only is this shorter, but it is also more flexible. Try out a different word such as freezing fog. Still works, right?

  3. 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, and the typical indentation is 4 spaces. There is not additional special word needed to end the loop, just change the indentation back to normal. for loops are useful to repeat some part of the code a definite number of times.

    Note

    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 :).

    Need to repeat some part of a program? Consider using a for loop!

  4. Let’s consider another example.

    In [13]: length = 0
    
    In [14]: for letter in 'blizzard':
       ....:     length = length + 1
       ....: 
    
    In [15]: print('There are', length, 'letters')
    There are 8 letters
    

    Can you follow what happens in this loop?

  5. 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.

    In [16]: letter = 'x'
    
    In [17]: for letter in 'sleet':
       ....:     print(letter)
       ....: 
    s
    l
    e
    e
    t
    
    In [18]: print('After the loop, letter is', letter)
    After the loop, letter is t
    
  6. 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.

    In [19]: 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

    In [20]: 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:
     |  
     |  __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
    

    Attention

    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
    

    Select your answer from the poll options at https://geo-python.github.io/poll/.

  7. 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.

    In [21]: mylist = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    
    In [22]: print(mylist)
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    
    In [23]: for i in range(6):
       ....:     mylist[i] = mylist[i] + i
       ....: 
    
    In [24]: 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?

  8. 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.

    In [25]: for i in range(len(mylist)):
       ....:     mylist[i] = mylist[i] + i
       ....: 
    
    In [26]: 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.

    In [27]: mylist.append(18.0)
    
    In [28]: mylist.append(21.0)
    
    In [29]: print(mylist)
    [0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0]
    
    In [30]: for i in range(len(mylist)):
       ....:     mylist[i] = mylist[i] + i
       ....: 
    
    In [31]: 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.

    Attention

    What output would the following program produce?

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

    Select your answer from the poll options at https://geo-python.github.io/poll/.