Section 5.5 Definite loops using for
The while
loop is ideal for those situations when we don’t know exactly how many iterations will be needed. There are, however, many instances when we know exactly how many times we need to go through a loop:
A loop to calculate the average daily temperature for a week will need exactly 7 iterations.
A loop to calculate quarterly sales totals will need exactly 4 iterations.
Processing a list of the population of states in the United States will need exactly 50 iterations.
Printing a list of the names of the months (Gregorian calendar) will need exactly 12 iterations.
In instances like these, when we want to loop through a set of things and we know how many entries there are in the set, we can construct a definite loop using a for
statement. The for
loop is a definite loop because it runs through as many iterations as there are items in the set.
Checkpoint 5.5.1.
Q-1: The while loop is an “indefinite” loop because…
it loops through a known set of items so it runs through as many iterations as there are items in the set.
Incorrect! A while loop does not run through a known set of items. Try again.
it loops until some condition becomes True.
Incorrect! A while loop will keep iterating while the condition is True. Try again.
it loops until some condition becomes False.
Correct! A while loop continues until the condition is False. There is no definite answer to when that happens; it all depends on the incrementation.
it loops until it reaches a "break" statement.
Incorrect! A while loop can stop at a break statement, but so can any other loop. Try again.
Checkpoint 5.5.2.
Q-2: The for loop is not an “indefinite” loop because…
it loops through a known set of items so it runs through as many iterations as there are items in the set.
Correct! A for
loop only runs through a distinct set of items.
it loops until some condition becomes True
.
Incorrect! A for
loop does not check any condition to decide whether it should continue to iterate. Try again.
it loops until some condition becomes False
.
Incorrect! This is the condition for a while
loop to stop. Try again.
The syntax of a for
loop is similar to the while
loop in that there is a for
statement and a loop body:
In Python terms, the variable
friends
is a
list of three strings. (We will examine lists in more detail in
Chapter 9.) The
for
loop goes through the list and executes the body once for each of the three strings in the list, resulting in this output:
Greetings, Joseph
Greetings, Vinh
Greetings, Shashi
Done!
Translating this for
loop to English is not as direct as the while
, but if you think of friends as a set, it goes like this: “Run the statements in the body of the for
loop once for each friend
in the set named friends
.”
Here is the for
loop all by itself:
for friend in friends:
print('Greetings,', friend)
In this for
loop, for and in are reserved Python keywords. friend
is the iteration variable for the loop. The variable friend
changes for each iteration of the loop. The iteration variable steps successively through the three strings stored in the friends
variable. The for
loop ends when the iteration variable has stepped through all the entries in the list.
Checkpoint 5.5.3.
Construct a block of code that prints “Hello, Prisha”, “Hello, Kahlil”, “Hello, Nirav”, “Hello, Aliyah”, and “Hello, Antonella”. After saying hello to each name in the list, print “All done!”. Watch out for three extra pieces of code and make sure your indentation is correct.
names = ['Prisha', 'Kahlil', 'Nirav', 'Aliyah', 'Antonella']
---
for name in names:
---
for names in names: #paired
---
print("Hello,", name)
---
print("Hello", name) #paired
---
print("All done!")
---
print(All done!) #paired