Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 3 · Lesson 1 · Core

for loops and range: repeating a set number of times

Write a for loop, use range with one, two or three values and count how many times it runs.

  • 55 minutes
  • Serves O3
  • Assumes Module 2

Phase 1: Prepare

Step 1 Warm-up recall

From Module 2: what does an if statement need after its condition?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • write a for loop over range (Foundation);
  • list the values range produces (Core);
  • count how many times a loop runs (Core).

Phase 2: Teach

Step 3 A familiar example

A teacher who writes a name on twenty exercise books does the same action twenty times. A for loop is the instruction: for each book, write the name.

Step 4 Clear explanation

A for loop repeats its indented lines once for each value in a sequence. The loop variable takes the next value on each pass.

for i in range(3):
    print("Hello", i)
print("Done")
Hello 0
Hello 1
Hello 2
Done
Next valuei takes 0, 1, 2Run blockindented linesMore?go backDonecontinue below
A for loop repeats its indented lines once for each value in a sequence such as range(3).

range makes the sequence of numbers. It counts from 0 by default and never reaches the stop value.

CallValues producedHow many
range(5)0, 1, 2, 3, 45
range(1, 6)1, 2, 3, 4, 55
range(0, 10, 2)0, 2, 4, 6, 85
range(3, 3)none0

A for loop also works on text, one character at a time: for ch in "abc": runs three times. The line under the loop that is not indented, such as print("Done"), runs once after the loop ends.

Step 5 Worked example (4 marks)

How many times does for k in range(2, 7): run, and what are the values?

  1. range(2, 7) starts at 2 and stops before 7. (1 mark)
  2. Values: 2, 3, 4, 5, 6. (1 mark)
  3. Count them: 5. (1 mark)
  4. Answer: 5 times, with k from 2 to 6. (1 mark)

Go deeper: the professor's notes

A for loop repeats a block once per value. Decide the values first, then the block.
Values

From range.

Block

Indented.

After

Runs once.

ChoosevaluesWriteblockIndentbodyRunthen trace
For loop

range

CallValues
range(3)0, 1, 2
range(1, 4)1, 2, 3

At work: A repeated switch

A switch must be checked 5 times.

  1. Use range(5).
  2. Place the check in the block.
  3. Test the first and last pass.

What a careful practitioner decides. State the block and exactly what ends it.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. range(4) produces:

2. range(1, 6) runs a loop:

3. The values of range(0, 10, 2) are:

4. A line after the loop that is not indented runs:

Phase 4: Close

Step 7 Recap

  • A for loop repeats its indented block for each value.
  • range(start, stop, step) never reaches stop.
  • Count values as stop minus start when the step is 1.

Step 8 Exit ticket (ungraded)

List the values of range(3, 8) and say how many times a loop over it runs.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Write a loop that prints the five times table from 5 to 50.

In industry: AT&T, 15 January 1990

On 15 January 1990 a software fault in AT&T's long-distance switches caused about half of the calls on its network to fail. Accounts describe a break statement in a loop that ended the wrong block of code, and about 70 million calls failed over nine hours.

Link to this lesson. A loop is a block that repeats. What belongs inside it, and what ends it, must be exact. Python makes the block visible by indentation.

Source: The New York Times, 16 Jan 1990; cs4fn, The AT&T crash; Sterling, The Hacker Crackdown

Common mistakes

MistakeWhy it fails
Expecting range(5) to include 5It stops at 4.
Forgetting the colonThe for line ends with a colon.
Indenting the line meant to run onceOnly the repeated lines are indented.

Limits. This lesson uses range and text; other sequences appear in Module 4.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What does a for loop do?
Next valueiRunblockDoneafter
for loop

It repeats its indented block once for each value in a sequence, such as range(5). It suits tasks with a known count, such as processing every row of a weekly report.

Card 2 of 3 · What values does range(1, 6) produce?
Start1Stop6 not reachedCount5
range

1, 2, 3, 4, 5. The stop value is never reached, so the loop runs 5 times. Mixing this up causes off-by-one errors in reports.

Card 3 of 3 · What does the third number in range(0, 10, 2) mean?
Start0Step2Stop10 not reached
Step

The step. The loop counts 0, 2, 4, 6, 8. Steps are useful for every second row or for multiples such as a times table.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards