Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
4.7. Lists and
A list is a sequence of items, and in a later chapter you will learn all about various ways to create, build and edit lists. For now, let’s focus on using lists. A list traversal uses a
for loop to iterate over each item in the list automatically. Consider this example:
It almost reads like natural language: For (each) fruit in (the list of) fruits, print (the name of the) fruit.
4.7.1. Using the range Function to Generate a Sequence to Iterate Over¶
We are now in a position to understand the inner workings we glossed over previously when we first introduced repeated execution with a for loop. Here was the example:
range function takes an integer n as input and returns a sequence of numbers, starting at 0 and going up to but not including n.
Thus, instead of
range(3), we could have written
[0, 1, 2].
The loop variable
_ is bound to 0 the first time lines 4 and 5 execute. The next time,
_ is bound to 1. Third time, it is bound to 2.
_ is a strange name for a variable but if you look carefully at the rules about variable names, it is a legal name. By convention, we use the
_ as our loop variable when we don’t intend to ever refer to the loop variable. That is, we are just trying to repeat the code block some number of times (once for each item in a sequence), but we are not going to do anything with the particular items.
_ will be bound to a different item each time, but we won’t ever refer to those particular items in the code.
By contrast, notice that in the previous activecode window, the loop variable is
afruit. In that for loop, we do refer to each item, with
print(afruit). When we are going to refer to the items and do something with them, it is important to use a loop variable name that is descriptive, such as
4.7.2. Iteration Simplifies our Turtle Program¶
Remember the turtle drawings we made earlier? Let’s look again at how we can use for loops there!
To draw a square we’d like to do the same thing four times — move the turtle forward some
distance and turn 90 degrees. We previously used 8 lines of Python code to have a turtle draw the four
sides of a square. This next program does exactly the same thing but, with the help of the for
statement, it uses just three lines (not including the setup code). Remember that the for statement
will repeat the
left four times, one time for each value in the list.
While “saving some lines of code” might be convenient, it is not the big deal here. What is much more important is that we’ve found a “repeating pattern” of statements, and we reorganized our program to repeat the pattern.
The values [0,1,2,3] were provided to make the loop body execute 4 times. We could have used any four values. For example, consider the following program.
In the previous example, there were four integers in the list. This time there are four strings.
Since there are four items in the list, the iteration will still occur four times.
take on each color in the list. We can even take this one step further and use the value of
aColor as part of the computation.
In this case, the value of
aColor is used to change the color attribute of
aColor to change to the next value in the list.
The for-loop is our first example of a compound statement. Syntactically a compound statement
is a statement. The level of indentation of a (whole) compound statement is the indentation of its
heading. In the example above there are five statements with the same indentation, executed
sequentially: the import, 2 assignments, the whole for-loop, and
for-loop compound statement is executed completely before going on to the next sequential
Check your Understanding