Year 10 Computing — 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.A list called xs holds 8 values. Which one of these reads the last of them?
- a) xs[7]
- b) xs[9]
- c) xs[len(xs)]
- d) xs[8]
2.This program reads the very first value out of a list. What does it print?
marks = [6, 1, 8, 3] print(marks[0])
3.A list can hold a single value and still be a list. What does this program print?
only = [17] print(len(only) + only[0])
4.How many values does the list in this program hold?
counts = [5, 2, 9, 4, 7, 1] print(len(counts))
5.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[len(xs)]
- xs[5]
- xs[0]
- xs[len(xs) - 1]
- xs[4]
- xs[2]
6.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])
7.This program works out the last position for itself rather than being told it. What does it print?
temps = [21, 19, 24, 22, 20] print(temps[len(temps) - 1])
8.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)
Answer key — Year 10 Computing — Lists and their positions
- 1. a) xs[7]
- 2. 6
- 3. 18
- 4. 6
- 5. 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
- 6. 19
- 7. 20
- 8. 4