Python Programming · Middle Stage (6–8) · 1 course, 15 worked problems, 36 practice questions
A list holds many values in order under one name, such as marks = [72, 65, 88]. Each item has an index that starts at 0, so marks[0] is 72 and marks[-1] is the last item. A list can change: .append() adds an item, .remove() takes one out, and you can replace an item by its index. A slice such as marks[1:3] gives part of a list. Functions like len(), sum(), max() and sorted() work on a whole list. With a loop, you can visit every item and process data such as readings from an experiment.
Choose an answer, then check it. Each one shows a full explanation.
A program keeps each letter of a word once, in a list, and then puts a star into the list. letters = [] for ch in "level": if ch not in letters: letters.append(ch) letters.insert(1, "*") print(letters, len(letters)) What does the program print?
A list comprehension picks some words from a list and turns each one round. Then the program takes a slice of one result. words = ["sun", "moon", "star", "sky"] picked = [w[::-1] for w in words if "s" in w] print(picked, picked[1][1:]) What does the program print?
A program changes one item of a list and then reads from it. colours = ["red", "green", "blue", "pink"] colours[1] = "gold" print(colours[-1], len(colours), colours[1]) What does the program print?
A program puts a new number into a list with insert(), and then asks the list two questions.
v = [2, 5, 2, 8]
v.insert(2, 7)
print(v.index(2), v.count(2), v)
What does the program print?
Each step of a staircase is a different height. In one line, the program works out how much higher every step is than the step below it. v = [3, 7, 8, 12] rise = [v[i] - v[i - 1] for i in range(1, len(v))] print(rise, max(rise), len(rise)) What does the program print?
A program deletes a slice with a step from a list, and then adds one item. v = [7, 14, 21, 28, 35, 42] del v[::2] v.append(v[0] + v[-1]) print(v) What does the program print?
A function sorts the list it is given, and then returns a slice of it. The caller changes the slice. def top_two(scores): scores.sort(reverse=True) return scores[:2] marks = [55, 90, 72, 81] best = top_two(marks) best.append(0) print(marks[0], len(marks), best) What does the program print?
Some list methods change the list and give back None. A function such as sorted() gives back a new list instead.
nums = [3, 1, 2]
a = nums.sort()
b = sorted(nums, reverse=True)
c = nums.append(4)
Which of these expressions are True after the code has run? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A function deletes an item of a list by its index, and returns the new length. It is called twice, and each result is printed on the same line. def remove_at(items, i): del items[i] return len(items) data = [10, 20, 30] print(remove_at(data, -1), end=" ") print(remove_at(data, 2), end=" ") print(data) What happens when the program runs?
A function builds a list from one column of a table that is a list of lists. Then the program changes both the new list and the table. def column(grid, k): return [row[k] for row in grid] g = [[1, 2, 3], [4, 5, 6]] c = column(g, 1) c[0] = 99 g[1][1] = 0 print(c, g[0][1]) What does the program print?
Each one has a hint, the key idea and a full worked solution.