Back to questions
JuniorTheoryCommon
What does the program print?
stock = {"apple": 3, "pear": 0}
print(stock.get("pear", 5), stock.get("plum"), "plum" in stock)0 None False
- Expecting
5for"pear": the default is used only when the key is missing, and"pear"is present with the value0, sogetreturns0. - Expecting a
KeyErrorfor"plum":getnever raises on a missing key; without a default it returnsNone. Onlystock["plum"]raises.
- What would
print(stock["plum"])do instead? - How would you add one to
stock["pear"]withget?