For Loops, online

Six activities your students do on their own devices. See who's finished, and their scores.

For Loops in one minute

What students need to know

A for loop repeats a block of code once for each item in a sequence — a list, a string, or a range() of numbers. The loop variable (often i or x) takes each value from the sequence in turn, connected by the keyword in: for i in range(5):. Like an if statement, the line must end with a colon and the repeated code must be indented underneath it.

Mistakes students make

  • Forgetting the trailing colon (for i in [1, 2, 3] instead of for i in [1, 2, 3]:) is the most common slip.
  • It's also easy to forget that range(5) produces 0, 1, 2, 3, 4 — five numbers starting at 0, not 1 through 5.
PREVIOUS · MODULE 19While Loops
YOU ARE HERE · MODULE 20For Loops
NEXT · MODULE 21range()

Discover other journeys with this module

What students will learn in this module

Every question in Module 20

Quick questions

  1. What does a for loop do?ALoops foreverBLoops through each item in a sequence
  2. for x in [10,20,30]:\n print(x)\nWhat prints?A10, 20, 30B30
  3. for char in 'Hi':\n print(char)\nWhat prints?AHiBH then i
  4. What keyword follows 'for x'?AinBof
  5. How many times does 'for i in range(3):' loop?A3 timesB4 times

Key terms

Students type the meaning of:

  • for
  • in
  • Loop variable
  • Sequence

Match the pairs

for x in range(5)
    print(x)
Match it to what it does
for in range(5):
    print('hi')
Match it to what it does
for char in 'hello':
    print(char)
Match it to what it does
for item in [1, 2, 3]:
    print(item)
Match it to what it does
for i in range(3):
    print(i)
Match it to what it does
total = 0
for n in [1,2,3]:
    total += n
print(total)
Match it to what it does

Fill in the blanks

  1. ___ i in [1, 2, 3]:
        print(i)
  2. for i __ [1, 2, 3]:
        print(i)
  3. for char in 'hello'_
        print(char)
  4. for color __ ['red', 'blue']:
        print(color)
  5. for i in _____(5):
        print(i)

Spot the error

Question 1

Atotal = 0Bfor i in [1, 2, 3]C    total += iD    print(i)Eprint('Sum:', total)

Question 2

Atotal = 0Bfor i in [1, 2, 3]:Cprint(i)D    total += iEprint('Done')

Question 3

Atotal = 0BFor i in [1, 2, 3]:C    total += iD    print(i)Eprint('Sum:', total)

Question 4

Atotal = 0Bfor i of [1, 2, 3]:C    total += iD    print(i)Eprint('Sum:', total)

Question 5

Atotal = 0Bfor i in 5:C    total += iDprint('Sum:', total)