QuestenaPractice that shows what to review next
Question 42

After `data = dict(a=1)` and `value = data.get("b", 9)`, what are `value` and the keys of `data`?

  1. `value` is `None`, and only `a` remains a key.
  2. `value` is 9, and only `a` remains a key.Correct answer
  3. `value` is 9, and both `a` and `b` become keys.
  4. The call raises `KeyError`, and the dictionary is unchanged.

Explanation

Because `b` is absent, `get` returns the supplied default 9, but it performs no assignment to the dictionary. Expecting a new key confuses a nonmutating lookup with methods that explicitly create missing entries.

Continue with this test

Start practice
Question 42: After `data = dict(a=1)` and `value = data.get("b", 9)`,… · Python Fundamentals · Questena