Editor

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
  1. Program 1

    d = {"a": 1, "b": 2, "a": 3}
    print(d, len(d))
  2. Program 2

    d = {"x": 1}
    print(d.get("y"), d.get("y", 0))
  3. Program 3

    d = {"x": 1}
    print(d["y"])
  4. Program 4

    d = {"apple": 3}
    print(3 in d, "apple" in d)
  5. Program 5

    counts = {}
    for ch in "hello":
        counts[ch] = counts.get(ch, 0) + 1
    print(counts["l"], len(counts))
  6. Program 6

    d = {"a": 1, "b": 2}
    for k in d:
        print(k, d[k] * 10)
  7. Program 7

    d = {"a": 1, "b": 2}
    v = d.pop("a")
    print(v, d)
  8. Program 8

    d = {"a": 1}
    d.setdefault("a", 9)
    d.setdefault("b", 7)
    print(d)