Year 10 Computing — Building a list as you go
Start from an empty list and add to it inside a loop, and work out how many values end up in it and what sits at each position.
Name: ________________________
1.A running total is appended each time round here, so the list holds the totals rather than the values. What does the program print?
totals = [] running = 0 for n in range(1, 5): running = running + n totals.append(running) print(totals[3])2.There are two appends inside this loop rather than one. How many values does the list end up with?
names = [] for n in range(1, 4): names.append(n) names.append(n * 10) print(len(names))3.This program starts with an empty list and puts one value into it each time round. How many values does it end up with?
squares = [] for n in range(1, 5): squares.append(n * n) print(len(squares))4.The program below is run again with each of these ranges written into its loop heading. Sort each range by how many values the list ends up holding.
kept = [] for n in range(0, 4): kept.append(n)Groups: Leaves 3 values · Leaves 5 values
- range(0, 5)
- range(10, 13)
- range(20, 25)
- range(7, 10)
- range(1, 6)
- range(2, 5)
5.This one prints the value that ended up at the front of the list. What does it print?
squares = [] for n in range(1, 6): squares.append(n * n) print(squares[0])6.This test lets some of the counter's values through and stops the rest. How many values does the list end up with?
kept = [] for n in range(0, 10): if n > 6: kept.append(n) print(len(kept))7.The program below is run again with each of these ranges written into its loop heading. Match each range to the value that ends up last in the list.
made = [] for n in range(0, 4): made.append(n * 3)- range(1, 4)
- range(2, 6)
- range(0, 3)
- range(4, 8)
- range(5, 9)
- 6
- 21
- 9
- 24
- 15
8.Only some of the counter's values get into this list. What does the program print?
found = [] for n in range(1, 8): if n * n > 20: found.append(n) print(found[0])- a) 6
- b) 5
- c) 4
- d) 25
Answer key — Year 10 Computing — Building a list as you go
- 1. 10
- 2. 6
- 3. 4
- 4. range(2, 5) → Leaves 3 values; range(0, 5) → Leaves 5 values; range(7, 10) → Leaves 3 values; range(1, 6) → Leaves 5 values; range(10, 13) → Leaves 3 values; range(20, 25) → Leaves 5 values
- 5. 1
- 6. 3
- 7. range(1, 4) → 9; range(2, 6) → 15; range(0, 3) → 6; range(4, 8) → 21; range(5, 9) → 24
- 8. b) 5