Year 11 Computing — Dictionaries and their keys
Look a value up in a dictionary by its key, and work out what adding a key or writing over one leaves behind.
Name: ________________________
1.A key the dictionary did not have is written to here. What does this program print?
stock = {"pen": 12, "book": 5} stock["bag"] = 9 print(len(stock))2.A dictionary called scores holds {"red": 14, "blue": 6, "green": 22, "black": 9}. Put these lookups in order of the number each one gives, smallest first.
- scores["blue"]
- scores["red"]
- scores["green"]
- scores["black"]
3.This dictionary starts with nothing in it at all. What does this program print?
seen = {} seen["pen"] = 2 seen["bag"] = 5 print(len(seen))4.You want to find one particular thing that a program has stored. What is the difference between a list and a dictionary here?
- a) A dictionary is looked in by position as well, starting from 0 like a list does
- b) A list can hold only numbers, and a dictionary only writing
- c) A list is looked in by position, and a dictionary by a key that whoever wrote the program chose
- d) A dictionary numbers its keys as they go in, so its first key is key 0
5.This line looks just like the last one, but the key it writes to is not new. What does this program print?
counts = {"red": 3, "blue": 8} counts["red"] = 10 print(len(counts))6.Two values are looked up and added together. What does this program print?
weights = {"box": 25, "bag": 40, "cup": 5} print(weights["box"] + weights["cup"])7.A value is looked up by name rather than by position here. What does this program print?
prices = {"pen": 10, "book": 45, "bag": 90} print(prices["book"])8.One of these two lines adds a pair and the other one does not. What does this program print?
stock = {"pen": 6} stock["book"] = 3 stock["pen"] = 11 print(stock["pen"] + stock["book"])
Answer key — Year 11 Computing — Dictionaries and their keys
- 1. 3
- 2. 1. scores["blue"] 2. scores["black"] 3. scores["red"] 4. scores["green"]
- 3. 2
- 4. c) A list is looked in by position, and a dictionary by a key that whoever wrote the program chose
- 5. 2
- 6. 30
- 7. 45
- 8. 14