FIGURE 7.1 Stepping Through a for Loop.
The loop continues until the_number has been assigned to the last number in the data list ( 5 ) and
the Python statement in the loop has been completed. Thus, all the numbers in the data list are used,
one at a time, in an iteration of the loop.
Watching for a Few “Gotchas”
You need to be careful about a couple potential problems with the for loop structure. The first
“gotcha” is forgetting to put a colon at the end of your for loop’s data list. Listing 7.2 shows the
error message you get as a result of making this mistake.
LISTING 7.2 A Missing Colon on a for Loop
Click here to view code image
>>> for the_number in [1, 2, 3, 4, 5]
File "<stdin>", line 1
for the_number in [1, 2, 3, 4, 5]
^
SyntaxError: invalid syntax
>>>
By the Way: Python Interactive Shell Versus Text Editor
When you are testing loop structure in a Python interactive shell, you need to press the
Enter key two times after the last Python statement in the loop. This alerts the