Class 9 Computer Science — Lists and their positions
Read a value out of a list by its position, count how many values a list holds, and work out which position the last value sits at.
Name: ________________________
1.In the list below, which position does the value 4 sit at?
ages = [3, 11, 4, 8]
- a) 3
- b) 1
- c) 0
- d) 2
2.The list is the same for all of these. Match each line to the number it prints.
values = [7, 4, 9, 2, 8, 3]
- print(values[0])
- print(values[2])
- print(values[5])
- print(len(values))
- print(values[len(values) - 2])
- 9
- 6
- 7
- 3
- 8
3.Two values are read out of this list and added together. What does it print?
nums = [5, 12, 7, 3] print(nums[1] + nums[3])
4.A list can hold a single value and still be a list. What does this program print?
only = [17] print(len(only) + only[0])
5.This one reads a value from further along the same kind of list. What does it print?
heights = [12, 15, 11, 19, 14] print(heights[3])
6.The list in this program holds five values. Sort each of these lines by whether it can be run at all.
xs = [3, 8, 1, 6, 2]
Groups: Reads a value out of the list · Falls off the end of the list
- xs[0]
- xs[2]
- xs[5]
- xs[4]
- xs[len(xs)]
- xs[len(xs) - 1]
7.This program prints the position the last value sits at, rather than the value itself. What does it print?
days = [2, 8, 5, 6, 9] print(len(days) - 1)
8.A list called xs holds 8 values. Which one of these reads the last of them?
- a) xs[len(xs)]
- b) xs[8]
- c) xs[7]
- d) xs[9]
Answer key — Class 9 Computer Science — Lists and their positions
- 1. d) 2
- 2. print(values[0]) → 7; print(values[2]) → 9; print(values[5]) → 3; print(len(values)) → 6; print(values[len(values) - 2]) → 8
- 3. 15
- 4. 18
- 5. 19
- 6. xs[0] → Reads a value out of the list; xs[4] → Reads a value out of the list; xs[5] → Falls off the end of the list; xs[len(xs) - 1] → Reads a value out of the list; xs[len(xs)] → Falls off the end of the list; xs[2] → Reads a value out of the list
- 7. 4
- 8. c) xs[7]