Grade 10 Computer Science — Changing and growing a list
Work out what a list holds after values have been added on the end, written over, or joined on from another list.
Name: ________________________
1.Two lists are joined into a third one here. What does this program print?
first = [1, 2] second = [7, 8, 9] both = first + second print(len(both))
2.A list called nums holds [2, 4, 6]. Each of these lines is run on its own, starting from that same list every time. Match each line to what nums holds afterwards.
- nums.append(8)
- nums[0] = 8
- nums[2] = 0
- nums = nums + [1, 3]
- nums[1] = 5
- [2, 4, 0]
- [2, 4, 6, 8]
- [8, 4, 6]
- [2, 5, 6]
- [2, 4, 6, 1, 3]
3.Two values are added on the end, one after the other. What does this program print?
bag = [3] bag.append(7) bag.append(2) print(bag[1] + bag[2])
4.A list called pins holds [8, 3]. Each of these lines is run on its own, starting from that list every time. Put them in order of how many values pins holds afterwards, fewest first.
- pins = pins + [1, 2]
- pins[0] = 4
- pins = pins + [6, 7, 0]
- pins.append(9)
5.A value is written over, and then the list is measured. What does this program print?
sizes = [5, 2, 9] sizes[0] = 40 print(len(sizes))
6.Something is added on the end and something else is written over. What does this program print?
row = [2, 4, 6] row.append(8) row[0] = 10 print(row[0] + row[3])
7.A value is written over here rather than added on. What does this program print?
scores = [3, 8, 1, 6] scores[1] = 10 print(scores[1] + scores[3])
8.Two lists are joined, and then a position is asked for out of the joined one. What does this program print?
a = [10, 20] b = [30, 40, 50] c = a + b print(c[3])
Answer key — Grade 10 Computer Science — Changing and growing a list
- 1. 5
- 2. nums.append(8) → [2, 4, 6, 8]; nums[0] = 8 → [8, 4, 6]; nums[2] = 0 → [2, 4, 0]; nums = nums + [1, 3] → [2, 4, 6, 1, 3]; nums[1] = 5 → [2, 5, 6]
- 3. 9
- 4. 1. pins[0] = 4 2. pins.append(9) 3. pins = pins + [1, 2] 4. pins = pins + [6, 7, 0]
- 5. 3
- 6. 18
- 7. 16
- 8. 40