Python Programming · High School (9–10) · 2 courses, 27 worked problems, 37 practice questions
A dictionary stores values under keys, as a glossary stores meanings under words. In Python you write marks = {"Asha": 72, "Ben": 65}, and marks["Asha"] gives 72. Keys must be unique and are often strings. Values can be anything, even lists or other dictionaries. You can add a pair, change a value, or remove a pair with del. The methods .keys(), .values() and .items() let a loop visit every pair. Looking up a key is fast, so dictionaries suit word counts, phone books and tables of elements.
.items() to count or total valuesChoose an answer, then check it. Each one shows a full explanation.
A price list is kept in a dictionary. The program changes one price, adds an item, and removes another. prices = {"tea": 20, "bun": 15} prices["tea"] = 25 prices["jam"] = prices.get("bun", 0) + 5 del prices["bun"] print(list(prices.keys()), sum(prices.values())) What does the program print?
A theatre stores its seats in a dictionary. Each key is a tuple of a row number and a seat letter. seats = {(1, "A"): "Ann", (1, "B"): "Raj", (2, "A"): "Mo"} row1 = [] for key in seats: if key[0] == 1: row1.append(key[1] + ":" + seats[key]) print(row1, (2, "A") in seats, ("A", 2) in seats) What does the program print?
A program should group words by their first letter, in a dictionary of lists. It stops with KeyError. The lines are numbered from 1.
groups = {}
for word in ["ant", "bee", "ape", "bat"]:
k = word[0]
groups[k].append(word)
print(groups)
Which changes make the program print {'a': ['ant', 'ape'], 'b': ['bee', 'bat']}? Choose two.
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 loop doubles every value of a dictionary, taking the keys in sorted order. Then the program looks at the dictionary in two ways. scores = {"b": 3, "a": 5, "c": 1} for k in sorted(scores): scores[k] = scores[k] * 2 print(list(scores.items())[0], sorted(scores.values())) What does the program print?
A function adds up points for the letters of a word. Letters that are not in the dictionary get a default value. def score(word, values): total = 0 for ch in word: total += values.get(ch, 1) return total pts = {"a": 2, "z": 10} print(score("pizza", pts), len(pts)) What does the program print?
A program updates a dictionary and asks it four questions. d = {"a": 1, "b": 2} d.update({"b": 20, "c": 30}) print(d.get("z", d["a"]), "20" in d, 20 in d.values(), len(d)) What does the program print?
A program counts the letters of a word in a dictionary, and then asks the dictionary three things. word = "abca" count = {} for ch in word: count[ch] = count.get(ch, 0) + 1 print(count, len(count), count.get("z")) What does the program print?
contacts = {"ravi": "98765"}. What does contacts.get("Ravi".lower()) give?
A message was coded with a shift of 19. Which shift, from 0 to 25, decodes it with the same function?
ranked = sorted([("Asha", 72), ("Ravi", 85), ("Meera", 64)], key=lambda p: -p[1]). What is ranked[0][0]?
Each one has a hint, the key idea and a full worked solution.