Recall & Review
beginner
What is a key in a Python dictionary?
A key is a unique identifier used to access a value in a Python dictionary. Think of it like a label on a box that helps you find what's inside.
Click to reveal answer
beginner
How do you access a value using a key in a Python dictionary?
You use square brackets with the key inside, like
dictionary[key]. This tells Python to find the value linked to that key.Click to reveal answer
beginner
What happens if you try to access a key that does not exist in a dictionary?
Python will give a
KeyError. It's like asking for a box that isn't there.Click to reveal answer
intermediate
How can you safely access a value for a key that might not exist?
Use the
get() method, like dictionary.get(key, default). It returns the value if the key exists, or a default value if it doesn't.Click to reveal answer
beginner
Example: Given
person = {'name': 'Alice', 'age': 30}, how do you get Alice's age?Use
person['age']. This will give you 30, the value linked to the key 'age'.Click to reveal answer
What will
my_dict['color'] return if my_dict = {'color': 'blue'}?✗ Incorrect
Accessing the key 'color' returns its value 'blue'.
What error occurs if you access a missing key in a dictionary using square brackets?
✗ Incorrect
Trying to access a key that doesn't exist raises a KeyError.
Which method allows safe access to a dictionary key with a default value?
✗ Incorrect
The get() method returns the value or a default if the key is missing.
If
data = {'x': 10}, what does data.get('y', 0) return?✗ Incorrect
Since 'y' is missing, get() returns the default value 0.
How do you access the value for key 'name' in
info = {'name': 'Bob'}?✗ Incorrect
Use square brackets with the key string: info['name'].
Explain how to access a value in a Python dictionary using a key.
Think about how you find a labeled box in a shelf.
You got /4 concepts.
Describe how to safely get a value from a dictionary when the key might not exist.
It's like asking politely and getting a backup answer.
You got /4 concepts.