Grade 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.A list can hold a single value and still be a list. What does this program print?
only = [17] print(len(only) + only[0])
2.Put these lines in order of the value each one gives, smallest first.
items = [40, 10, 30, 20]
- items[3]
- items[0]
- items[1]
- items[2]
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.How many values does the list in this program hold?
counts = [5, 2, 9, 4, 7, 1] print(len(counts))
5.A list called xs holds 8 values. Which one of these reads the last of them?
- a) xs[len(xs)]
- b) xs[7]
- c) xs[9]
- d) xs[8]
6.This program reads the very first value out of a list. What does it print?
marks = [6, 1, 8, 3] print(marks[0])
7.A value is copied from one position into another before anything is printed. What does this program print?
row = [1, 2, 3, 4] row[0] = row[3] print(row[0] + row[3])
8.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])
- 6
- 3
- 9
- 7
- 8
Answer key — Grade 9 Computer Science — Lists and their positions
- 1. 18
- 2. 1. items[1] 2. items[3] 3. items[2] 4. items[0]
- 3. 15
- 4. 6
- 5. b) xs[7]
- 6. 6
- 7. 8
- 8. print(values[0]) → 7; print(values[2]) → 9; print(values[5]) → 3; print(len(values)) → 6; print(values[len(values) - 2]) → 8