Home » Chapter 4 : Repetition
for Loops
On the previous page, I introduced you to the while loop and explained that it is a condition-controlled repetition structure. The while loop is a good choice when we want to repeat (iterate) statements based on a condition we can test. The for loop is different.
The for loop is a count-controlled repetition structure that iterates a specific number of times. There are several ways in which we can define the number of times a for loop will repeat, usually based on a sequence or range of values. I will describe them briefly first, then look closely at one of them in detail, then we will see more of them in detail as we proceed through this book.
for variable in [val_1, val_2, ... val_n]:
statement_1
statement_2
.
.
statement_n
Code Details:
Before we look at some examples, let's take a look at the Python range() function. The range() function returns a sequence of numbers based on start, stop and step (increment) values we specify. Here is the general form of the range() function:
range(start, stop, step)
Code Example | Output | Notes |
---|---|---|
|
0
|
Prints a range with only the stop parameter. |
|
10
|
Prints a range with the start and stop parameters. |
|
10
|
Prints a range with the start, stop and step parameters. Note that this produces a list of the tens from ten (start) to 100 (stop). The step parameters control the interval between each iteration of the loop. |
|
0
|
Prints the same range as the previous example above, except that we get 0 in the output when we set start to 0. |
|
5
|
Print a range from 5 to 50, by fives. Notice the pattern here when we set our start and step to the same number. |
When you are learning programming structures, especially those that repeat steps, it is often very useful to manually trace the execution of that structure. For loops this is called loop tracing. When we trace code execution it is important to, first, understand how the structure works and, second, to write out each step the way the interpreter will execute each step in the computer.
For example, consider the following for loop:
for i in range(5):
print(i)
To trace this loop, we must be aware that the range() function provides the number of iterations the loop will make. In this example, this for loop will iterate 5 times. We also must be aware that range values start at zero, so the values that the variable i will contain during the execution of this code will be 0, 1, 2, 3, and 4.
Iteration | i | i < 5 | print(i) |
---|---|---|---|
1 | 0 | True | 0 |
2 | 1 | True | 1 |
3 | 2 | True | 2 |
4 | 3 | True | 3 |
5 | 4 | True | 4 |
6 | 5 | False | Exit loop |
Write a Python program that uses a for loop and the range() function to print numbers from zero to 13 (including 13) using only one parameter in the range() function.
Write a Python program that uses a for loop and the range() function to print numbers from 11 to 20 (not including 20).
Write a Python program that uses a for loop and the range() function to print the numbers between 40 and 80 (inclusive) that are divisible by 4.
Write a Python program that prompts the user for the start, stop and step values for a for loop based on the range() function. Here's an example of the output from a possible solution. Notice the details of the user instructions, prompts, and the output statement contains the values they entered as part of the output statement for the user.
--------------------------------------------------
Enter integers for the following loop parameters:
Start: 10000
Stop: 10100
Step: 10
--------------------------------------------------
Here is your range(10000, 10100, 10) output:
--------------------------------------------------
10000
10010
10020
10030
10040
10050
10060
10070
10080
10090