For Loops, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
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 offor 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.
What students will learn in this module
Every question in Module 20
Quick questions
- What does a for loop do?ALoops foreverBLoops through each item in a sequence
- for x in [10,20,30]:\n print(x)\nWhat prints?A10, 20, 30B30
- for char in 'Hi':\n print(char)\nWhat prints?AHiBH then i
- What keyword follows 'for x'?AinBof
- How many times does 'for i in range(3):' loop?A3 timesB4 times
Key terms
Students type the meaning of:
forinLoop variableSequence
Match the pairs
for x in range(5)
print(x)Match it to what it doesfor in range(5):
print('hi')Match it to what it doesfor char in 'hello':
print(char)Match it to what it doesfor item in [1, 2, 3]:
print(item)Match it to what it doesfor i in range(3):
print(i)Match it to what it doestotal = 0
for n in [1,2,3]:
total += n
print(total)Match it to what it doesFill in the blanks
___ i in [1, 2, 3]: print(i)for i __ [1, 2, 3]: print(i)for char in 'hello'_ print(char)for color __ ['red', 'blue']: print(color)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)
