Dictionaries & Sets · Question 5 · Easy

A price is looked up with square brackets for an item that is not on the menu. What happens?

prices = {"tea": 12}
print(prices["coffee"])
  1. None
  2. KeyError
  3. 0
  4. NameError
Answer and explanation

Answer: B. KeyError

Explanation: Square-bracket lookup of a missing key raises KeyError. Only get() returns None for a missing key.

Previous: Which expression creates an empty set? · Next: The union and the intersection of two sets are measured. What does this print?

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge