Книга: Practical Programming, Fourth Edition
Назад: Chapter 9: Repeating Code Using Loops
Дальше: Processing Characters in Strings

Processing Items in a List

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 to the print function for each velocity in the list:

 >>>​​ ​​velocities​​ ​​=​​ ​​[0.0,​​ ​​9.81,​​ ​​19.62,​​ ​​29.43]
 >>>​​ ​​print(f​​'Metric: {velocities[0]} m/sec; '​​,
 ...​​ ​​f​​'Imperial: {velocities[0] * 3.28} ft/sec'​​)
 Metric: 0.0 m/sec; Imperial: 0.0 ft/sec
 >>>​​ ​​print(f​​'Metric: {velocities[1]} m/sec; '​​,
 ...​​ ​​f​​'Imperial: {velocities[1] * 3.28} ft/sec'​​)
 Metric: 9.81 m/sec; Imperial: 32.1768 ft/sec
 >>>​​ ​​print(f​​'Metric: {velocities[2]} m/sec; '​​,
 ...​​ ​​f​​'Imperial: {velocities[2] * 3.28} ft/sec'​​)
 Metric: 19.62 m/sec; Imperial: 64.3536 ft/sec
 >>>​​ ​​print(f​​'Metric: {velocities[3]} m/sec; '​​,
 ...​​ ​​f​​'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(f​​'Metric: {velocity} m/sec;'​​,
 ...​​ ​​f​​'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:
  body

A for loop is executed as follows:

As you saw in , a body is just a sequence of one or more statements. Each pass through the body is referred to as an iteration. At the start of each iteration, Python assigns the next item on the list to the loop variable. In the same vein as with function definitions and if statements, the statements in the loop body are indented.

In the previous code, before the first iteration, the variable velocity is assigned the value of velocities[0], and then the loop body is executed. Before the second iteration, velocity is assigned the value of velocities[1], and then the loop body is executed, and so on. In this way, the program can process each item sequentially. Table 7, , contains the value of velocity at the start of each iteration, as well as the output printed during that iteration.


Table 7. Looping Over List Velocities

Iteration

List Item Referred to at
Start of Iteration

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, you created a new variable, velocity, to refer to the current item of the list inside the loop. You could have equally well used an existing variable.

If you 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, precisely as if you 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.

Назад: Chapter 9: Repeating Code Using Loops
Дальше: Processing Characters in Strings