Introduction:
A for
loop is a construct in Python that lets you repeat a certain task over and over again, automatically. Imagine you wanted to do something simple, like printing "Hello!" on the screen 10 times. Instead of typing out the print statement 10 times, you can use a for loop to do it for you—much faster and much easier to read!
How is a For Loop Set Up?
A for loop uses this basic structure:
for i in range(5): print(i)
Here’s what each part means:
- for: This is the command that starts the loop.
- i: This is a variable that will take on different values each time the loop runs. You can think of it like a counter or a placeholder for numbers.
- range(5): This tells the loop how many times to run. In this case,
range(5)
means the loop will count from 0 to 4 (five numbers in total, starting from 0). The loop will stop just before reaching 5. - print(i): This is the task you want to repeat. In this example, the task is to print the value of
i
, which in this example would be:
0
1
2
3
4
Real-World Example
Let’s say you're organising a game for your class and you need to display the names of all the students in a list. Instead of writing a print()
statement for each student, you can use a for loop to go through the list automatically and print each name. That’s one way for loops help you in real life—automating repetitive tasks!
What About the range()
?
The function range()
controls how many times the loop runs and what values the variable (i
) takes. In its simplest form, like range(5)
, it starts at 0 and goes up to (but doesn't include) 5.
But range()
can do much more. You can also tell it:
- Where to start: You don’t have to start at 0. For example,
range(1, 6)
will count from 1 to 5. - Step size: You can count by more than 1. For example,
range(0, 10, 2)
counts jumping by 2 each time, so here it would count from 0 to 8, because the next step would be 10, and we don't include the upper range value (so it prints 0, 2, 4, 6, 8).
Here's a quick example showing how you can change the step value:
for i in range(0, 10, 2): print(i)
In this case, the loop prints 0, 2, 4, 6, and 8. The third argument (the "step") in range()
tells Python to count by 2s instead of 1s. You can also use negative numbers to count downwards.
Why Use For Loops?
For loops are incredibly useful because they let you save time and avoid mistakes. Instead of copying and pasting the same code over and over again, you can use a for loop to repeat it as many times as you need. Plus, if you need to change something, you only have to do it once!
Predict:
Let’s start by thinking about the following code. You don't need to run it yet—just try to predict what it will do.
What do you think will happen?
Take a moment to think before moving on to the next section.
for i in range(3): print("Python is awesome!")
Run:
Now let’s run a simple piece of code to see how a for loop works. This code will count from 1 to 5 and print each number:
Run the code in the embedded IDE and see how the for loop counts from 1 to 5. Notice how the number inside range()
determines how many times the loop runs.
Investigate:
Let’s take a closer look at how we can make the loop count by steps other than 1. Run the following code:
What do you observe?
In this code, the loop starts at 0, but it counts up by 2 each time, printing only even numbers. You can investigate further by changing the numbers inside range()
—try counting backwards or using different steps.
Modify:
Now let’s see if you can fix a common mistake when using for loops with range()
. The following code is supposed to count from 1 to 5, but there is an error. Can you spot and correct it?
Hint: The range starts at 0 by default. You'll need to adjust it so the loop starts from 1.