Looping through a list with for...in

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Looping through a list with for...in

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly loops through a list called pets?
A for pet in pets
B for pet in pets:
2. scores = [5, 8, 9] for s in scores: print(s) How many times does print(s) run?
A 3 times, once for each value in the list
B 1 time, only for the first value
3. In the loop for fruit in fruits:, what does the variable fruit hold?
A The current value from the list on each time round
B The whole list all the time
4. Which loop prints every name in the list names?
A for n in names: print(n)
B for n in names: print(n)
5. colours = ["red", "blue"] for c in colours: print(c) What is printed first?
A blue
B red

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for pet in pets: AThe if line is not indented, so it is not inside the loop. It needs 4 spaces before it.
2for score in scores: if score > 50: BRuns the indented block once for each value in the list pets, with pet holding the current value each time
3The item variable holds the current value from the list each time round the loop CLoops through every score in the list and uses an if to check each one against 50
4Looping with an if inside only counts the items that pass the test DThe first line is missing the colon at the end. It should be for fruit in fruits:
5for fruit in fruits print(fruit) Efor age in ages: if age >= 13: count = count + 1
6for mark in marks: if mark > 50: print(mark) Ffor name in names: print(name)

Definitions

for item in my_list:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ item in my_list: print(item)
2.
for item __ my_list: print(item)
3.
for fruit in fruits: print(_____)
4.
for mark in marks: if mark __ 50: print(mark)
5.
for n in numbers: __ n > 10: print(n)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A pets = ["cat", "dog", "fish"]
B for pet in pets:
C print(item)
Question 2
A nums = [4, 9, 12, 7]
B total = 0
C for n in nums:
D total = total + nums
E print(total)
Question 3
A fruits = ["apple", "pear"]
B for fruit in fruits
C print(fruit)
Question 4
A ages = [10, 15, 13, 18]
B for age in ages:
C if age >= 13:
D print(ages, "teen")
Question 5
A marks = [45, 80, 62]
B for mark in marks:
C if mark = 50:
D print("Pass")

Answer Key

Looping through a list with for...in
Section A: Quick Questions
Q1
B — for pet in pets:
Q2
A — 3 times, once for each value in the list
Q3
A — The current value from the list on each time round
Q4
A — for n in names: print(n)
Q5
B — red
Section B: Match the Pairs
Q1
for pet in pets: → Runs the indented block once for each value in the list pets, with pet holding the current value each time
Q2
for score in scores: if score > 50: → Loops through every score in the list and uses an if to check each one against 50
Q3
The item variable holds the current value from the list each time round the loop → for name in names: print(name)
Q4
Looping with an if inside only counts the items that pass the test → for age in ages: if age >= 13: count = count + 1
Q5
for fruit in fruits print(fruit) → The first line is missing the colon at the end. It should be for fruit in fruits:
Q6
for mark in marks: if mark > 50: print(mark) → The if line is not indented, so it is not inside the loop. It needs 4 spaces before it.
Section C: Definitions
Q1
for item in my_list: — Runs the indented block once for each value in the list. The variable item holds the current value each time round, starting with the first and finishing with the last.
Section D: Fill in the Blanks
Q1
for item in my_list: print(item)
Q2
for item in my_list: print(item)
Q3
for fruit in fruits: print(fruit)
Q4
for mark in marks: if mark >= 50: print(mark)
Q5
for n in numbers: if n > 10: print(n)
Section E: Spot the Error
Q1
C — The loop variable is called pet, so item does not exist and causes a NameError. Inside the loop, use the loop variable: print(pet).
Q2
D — nums is the whole list, so you cannot add it to a number. Add the loop variable n, which holds one value each time: total = total + n.
Q3
B — A for line must end with a colon. It should be 'for fruit in fruits:'.
Q4
D — ages is the whole list. Inside the loop, use the item variable age, which holds the current value. It should be print(age, "teen").
Q5
C — A single = assigns a value. An if needs a comparison operator. It should be 'if mark >= 50:'.