Class 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.A list holds four values, and a program runs the line values[4] = 7. Why is that a mistake?
- a) There is no position 4 yet, and writing to a position can only change a value that is already there
- b) The list would end up five values long with a gap in the middle of it
- c) Position 4 is the first position, and it already holds a value
- d) Writing to a position is never allowed once a list has been made
2.Something is added on the end, and then a position from before the change is asked for. What does the program print?
marks = [12, 7, 20] marks.append(5) print(marks[2])
3.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])
4.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))
5.A list called box already holds four values. Sort each line by what it does to how long the list is.
Groups: Makes the list longer · Leaves the length alone
- box[0] = 7
- box.append(7)
- box.append(12)
- box[3] = 1
- box[2] = 9
- box.append(0)
6.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))
7.One value is added on the end of this list. What number does the program print?
queue = [4, 9] queue.append(6) print(len(queue))
8.A list holds five values. One of its values is written over, and then one more value is added on the end. How many values does the list hold now?
- a) 4
- b) 6
- c) 7
- d) 5
Answer key — Class 10 Computer Science — Changing and growing a list
- 1. a) There is no position 4 yet, and writing to a position can only change a value that is already there
- 2. 20
- 3. 18
- 4. 3
- 5. box.append(7) → Makes the list longer; box[0] = 7 → Leaves the length alone; box.append(0) → Makes the list longer; box[3] = 1 → Leaves the length alone; box.append(12) → Makes the list longer; box[2] = 9 → Leaves the length alone
- 6. 5
- 7. 3
- 8. b) 6