Dictionaries: What Does This Print
What does this print? · Intermediate · about 8 min
Read each short program and type exactly what it prints, with each printed line on its own line. If a program stops with an error, type the error's name, like TypeError.
Example
d = {"cat": 2} d["dog"] = 3 print(d["cat"], len(d))
prints
2 2
Program 1
d = {"a": 1, "b": 2, "a": 3} print(d, len(d))
Program 2
d = {"x": 1} print(d.get("y"), d.get("y", 0))
Program 3
d = {"x": 1} print(d["y"])
Program 4
d = {"apple": 3} print(3 in d, "apple" in d)
Program 5
counts = {} for ch in "hello": counts[ch] = counts.get(ch, 0) + 1 print(counts["l"], len(counts))
Program 6
d = {"a": 1, "b": 2} for k in d: print(k, d[k] * 10)
Program 7
d = {"a": 1, "b": 2} v = d.pop("a") print(v, d)
Program 8
d = {"a": 1} d.setdefault("a", 9) d.setdefault("b", 7) print(d)