Master the for loop to iterate over a specific range of numbers or items.
Imagine you were tasked with writing 'I will master Python' exactly 1,000 times. Would you rather spend hours typing, or write just two lines of code that finish the job in less than a second?
In programming, we often need to repeat a task multiple times. A for loop is used for definite iteration, which means we know exactly how many times the code should run before it even starts. Think of it like a train track with a specific number of stations; the train (your code) must stop at every station until it reaches the end. In Python, we use the `for` keyword combined with the `range()` function to tell the computer how many 'stations' to visit. The variable we define in the loop, often called (for index), acts as a counter that keeps track of which iteration we are currently on.
Let's print the numbers 0 through 4. 1. Write the loop header: `for i in range(5):` 2. Indent the next line and write: `print(i)` 3. The output will be: 0 1 2 3 4 Note that Python starts counting at and stops before the number .
Quick Check
If you write 'for x in range(10):', what is the very last number that will be printed?
Answer
9
The `range()` function is more powerful than it looks. It can actually take three different 'arguments' or inputs: start, stop, and step. The syntax looks like this: `range(start, stop, step)`. - Start: The first number in your sequence (default is ). - Stop: The limit where the loop ends (not included in the output). - Step: How much to add to the number each time (default is ). By changing the step, you can count by twos, tens, or even count backwards by using a negative number like !
Suppose you want to print even numbers between 10 and 20. 1. Set the start to . 2. Set the stop to (so that 20 is included). 3. Set the step to . Code: `for n in range(10, 21, 2):` This results in: .
Quick Check
How would you write a range that counts backwards from 5 down to 1?
Answer
range(5, 0, -1)
Choosing the right tool makes coding easier. Use a for loop when you know the number of repetitions in advance (e.g., 'Do this 10 times' or 'Do this for every item in a list'). Use a while loop when you don't know how long it will take, and you are waiting for a specific condition to change (e.g., 'Keep playing the game until the player hits a wall'). A for loop is like a scheduled bus route, while a while loop is like a taxi driving until the passenger says 'Stop!'
Create a countdown for a rocket launch that only prints the odd numbers from 9 down to 1, then says 'Blast off!' 1. Use `range(9, 0, -2)` to start at 9 and skip even numbers. 2. Inside the loop, `print(i)`. 3. Outside the loop (unindented), `print('Blast off!')`. This combines starting, stopping, and stepping in reverse.
What is the default starting number for `range(5)`?
Which code will print the numbers 5, 10, and 15?
A for loop is the best choice for a program that runs until the user types 'quit'.
Review Tomorrow
Tomorrow, try to explain to a friend why range(1, 10) only prints 9 numbers instead of 10.
Practice Activity
Write a Python script that uses a for loop to calculate the sum of all numbers from 1 to 100.