Using "for" Loops
The for loop is useful when we know how many times a loop is to be executed. Rather than basing the number of iterations on a logical condition, the for loop controls the number of times a loop will iterate by counting or stepping through a series of values. Run this snippet of code in Repl.it:
for i in range(5): print(i)
range(5) command creates a series of five values from 0 to 4 for the variable
i to cycle through. The statement
for i in range(5) effectively means use the variable
i to count through five values starting at 0 and stopping at 4 (Python, by design, begins counting with the value 0). Also, once again, pay close attention to the placement of the colon (:) at the end of the for statement as well as the indentation. The syntax rules should look very familiar at this point. They are exactly the same as for if statements and while loops.
Consider this next snippet of code:
n=8 sumval=0; for i in range(n): sumval = sumval+i print('Adding ', i,'to the previous sum = ',sumval)
This loop counts from 0 to
n has been set to a value of 8. An initial sum is set to zero and then successive values of the variable
i are added to the sum each time an iteration of the loop takes place. In other words, the loop counter variable
i can be used within the loop as it is being updated. As will we see, the loop variable is often used as part of some computation within the loop. Here is an example of how powerful Python can be when using a for loop to cycle through a series of values.
strval='forever' e_count=0 for letter in strval: if letter =='e': e_count = e_count + 1 print('The letter e occurred ', e_count, 'times')
Notice how the "if" statement is "nested" (and, therefore, indented) within the "for" loop. The variable
letter cycles through each character in the string
strval, which has been initialized to the string
forever. If an "e" is found while iterating through each character, the count variable is incremented. When the loop is finished, the number of occurrences is output to the screen.
It is also possible to nest loops within loops. We will discuss more of these types of loops as we go deeper into the course. For now, run this code in Repl.it:
m=4 n=3 for i in range(m): for j in range(n): print('i=',i,' j=',j)
This is called a nested "for" loop because the inner "for" loop depending on the variable
j is nested within the outer "for" loop, which depends on the variable
i. This should be clear from the indentation of the inner loop with respect to the outer loop. For each iteration of the variable
i, the variable
j cycles through all of its possible values which depend upon
range(m). In order for
ito be updated to its next value, the inner loop must iterate through all of its values. Make sure you can relate the program output shown the screen to how the nested loop iterates through its values.
After you watch the video and test the examples in Repl.it, revisit the file in the "while" loop section and read through the section on "for" loops. Make sure to practice the for loop examples in Repl.it.