For loops

In progamming, we often want to repeat the same operation many times without having to re-type the same instructions.

west

The problem

Let’s say someone had provided us with a list of of states nearby but they didn’t capitalize the first letter!

states = ['washington', 'oregon', 'california', 'arizona', 'nevada', 'idaho', 'utah']

We could use the string method .capitalize() to change the first letter of each item in the list to uppercase like so:

state1 = states[0].capitalize()
state2 = states[1].capitalize()
state3 = states[2].capitalize()
state4 = states[3].capitalize()
state5 = states[4].capitalize()
state6 = states[5].capitalize()
state7 = states[6].capitalize()

states_capitalized = [state1, state2, state3, state4, state5, state6, state7]
states_capitalized
['Washington', 'Oregon', 'California', 'Arizona', 'Nevada', 'Idaho', 'Utah']

But what if this list contained 100 or 1000 place names? It would take a long time (and be extremely boring) to type this all out. When you find yourself repetivitely typing the same thing but only changing one number of character… you know it’s time to write a for loop!

Basic for loop format

For loops iterate over lists, repeating the same instructions for each element. We can write one like this:

# Define empty list
states_capitalized = []

for state in states:
    
    # Capitalize place name
    new_name = state.capitalize()
    
    # Append to new list
    states_capitalized.append(new_name)

states_capitalized
['Washington', 'Oregon', 'California', 'Arizona', 'Nevada', 'Idaho', 'Utah']

OK so what happened here? There are a couple of things going on so let’s break down what we did.

  • We defined an empty list (to put our capitalized place names)

  • We initiated the loop using the standard for item in list syntax. This basically means “…for every item in the list”. Note that this statement must end with a colon (:).

  • The code that is executed as part of the loop must be indented beneath the for loop statement. The typical indentation is four space keys or one Tab key.

  • For every state, we:

    1. Capitalized the the first letter of the state name.

    2. Added the capitalized state name to our empty list using the .append method.

Note that the variables state and new_name used in the for loop are just normal variables and still exist after the loop has completed. They are equal to the last item in the list.

state
'utah'
new_name
'Utah'

Looping with an index

We can use a loop to iterate over any collection of values in Python. For example, we can also write a loop that performs a calculation for a sequence of integers using the built-in function called range.

range(5)
range(0, 5)

Tip

range can accept one, two, or three parameters.

  • If one parameter is given, range generates a sequence of that length, starting at zero and incrementing by 1. For example, range(3) produces the numbers 0, 1, 2.

  • If two parameters are given, range starts at the first and ends just before the second, incrementing by one. For example, range(2, 5) produces 2, 3, 4.

  • If range is given three parameters, it starts at the first one, ends just before the second one, and increments by the third one. For example, range(3, 10, 2) produces 3, 5, 7, 9

We can write the same for loop as before to capitalize the state names.

# Define empty list
states_capitalized = []

for i in range(7):
    
    # Print iteration
    print(i)
    
    # Capitalize place name
    new_name = states[i].capitalize()
    
    # Append to new list
    states_capitalized.append(new_name)

states_capitalized   
0
1
2
3
4
5
6
['Washington', 'Oregon', 'California', 'Arizona', 'Nevada', 'Idaho', 'Utah']

This time we looped over every integer between 0 and 4 (i.e. 0, 1, 2, 3, 4). We used this integer to index every item in our list and capitalize it.

Note

The variable i is commonly used to denote the index variable in loops. If we have multiple for loops in our script, we might consider using j or k etc.

Looping through two lists

This might seem a strange, convoluted way of doing the same thing as before but it is quite useful. Let’s say we had another list containing the total area of these states:

area = [184661, 254799, 423967, 295234, 286380, 216630, 219890]

We could use the index approach to print pairs in a single loop:

for i in range(len(states_capitalized)):
    
    print(f"The total area of {states_capitalized[i]} is {area[i]} square kilometers")
The total area of Washington is 184661 square kilometers
The total area of Oregon is 254799 square kilometers
The total area of California is 423967 square kilometers
The total area of Arizona is 295234 square kilometers
The total area of Nevada is 286380 square kilometers
The total area of Idaho is 216630 square kilometers
The total area of Utah is 219890 square kilometers

Note

In the example above, we used the length of the states list in the range() function but we could have just as easily used the length of the area list to define i since both lists are the same length.