With what you’ve learned so far, to print the items from a list of velocities of falling objects in metric and Imperial units, you would need to write a call on function print for each velocity in the list:
| >>> velocities = [0.0, 9.81, 19.62, 29.43] |
| >>> print('Metric:', velocities[0], 'm/sec;', |
| ... 'Imperial:', velocities[0] * 3.28, 'ft/sec') |
| Metric: 0.0 m/sec; Imperial: 0.0 ft/sec |
| >>> print('Metric:', velocities[1], 'm/sec;', |
| ... 'Imperial:', velocities[1] * 3.28, 'ft/sec') |
| Metric: 9.81 m/sec; Imperial: 32.1768 ft/sec |
| >>> print('Metric:', velocities[2], 'm/sec; ', |
| ... 'Imperial:', velocities[2] * 3.28, 'ft/sec') |
| Metric: 19.62 m/sec; Imperial: 64.3536 ft/sec |
| >>> print('Metric:', velocities[3], 'm/sec; ', |
| ... 'Imperial:', velocities[3] * 3.28, 'ft/sec') |
| Metric: 29.43 m/sec; Imperial: 96.5304 ft/sec |
This code is used to process a list with just four values. Imagine processing a list with a thousand values. Lists were invented so that you wouldn’t have to create a thousand variables to store a thousand values. For the same reason, Python has a for loop that lets you process each element in a list in turn without having to write one statement per element. You can use a for loop to print the velocities:
| >>> velocities = [0.0, 9.81, 19.62, 29.43] |
| >>> for velocity in velocities: |
| ... print('Metric:', velocity, 'm/sec;', |
| ... 'Imperial:', velocity * 3.28, 'ft/sec') |
| ... |
| Metric: 0.0 m/sec; Imperial: 0.0 ft/sec |
| Metric: 9.81 m/sec; Imperial: 32.1768 ft/sec |
| Metric: 19.62 m/sec; Imperial: 64.3536 ft/sec |
| Metric: 29.43 m/sec; Imperial: 96.5304 ft/sec |
The general form of a for loop over a list is as follows:
| for variable in list: |
| block |
A for loop is executed as follows:
The loop variable is assigned the first item in the list, and the loop block—the body of the for loop—is executed.
The loop variable is then assigned the second item in the list and the
loop body is executed again.
...
Finally, the loop variable is assigned the last item of the list and the loop body is executed one last time.
As we saw in Defining Our Own Functions, a block is just a sequence of one or more statements. Each pass through the block is called an iteration, and at the start of each iteration, Python assigns the next item in the list to the loop variable. As with function definitions and if statements, the statements in the loop block are indented.
In the previous code, before the first iteration, variable velocity is assigned velocities[0] and then the loop body is executed; before the second iteration it is assigned velocities[1] and then the loop body is executed; and so on. In this way, the program can do something with each item in turn. Table 12, Looping Over List Velocities , contains the value of velocity at the start of each iteration, as well as what is printed during that iteration.
Iteration |
List Item Referred to at |
What Is Printed During This Iteration |
---|---|---|
1st |
velocities[0] |
Metric: 0.0 m/sec; Imperial: 0.0 ft/sec |
2nd |
velocities[1] |
Metric: 9.81 m/sec; Imperial: 32.1768 ft/sec |
3rd |
velocities[2] |
Metric: 19.62 m/sec; Imperial: 64.3536 ft/sec |
4th |
velocities[3] |
Metric: 29.43 m/sec; Imperial: 96.5304 ft/sec |
In the previous example, we created a new variable, velocity, to refer to the current item of the list inside the loop. We could have equally well used an existing variable.
If we use an existing variable, the loop still starts with the variable referring to the first element of the list. The content of the variable before the loop is lost, exactly as if we had used an assignment statement to give a new value to that variable.
The variable is left holding its last value when the loop finishes:
| >>> speed = 2 |
| >>> velocities = [0.0, 9.81, 19.62, 29.43] |
| >>> for speed in velocities: |
| ... print('Metric:', speed, 'm/sec') |
| ... |
| Metric: 0.0 m/sec |
| Metric: 9.81 m/sec |
| Metric: 19.62 m/sec |
| Metric: 29.43 m/sec |
| >>> print('Final:', speed) |
| Final: 29.43 |
Notice that the last print statement isn’t indented, so it is not part of the for loop. It is executed, only once, after the for loop execution has finished.