0
0
Pythonprogramming~20 mins

Accessing values using keys in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Key Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of accessing a nested dictionary value?
Given the dictionary data = {'user': {'name': 'Alice', 'age': 30}}, what is the output of print(data['user']['name'])?
Python
data = {'user': {'name': 'Alice', 'age': 30}}
print(data['user']['name'])
AKeyError
B30
CAlice
DNone
Attempts:
2 left
๐Ÿ’ก Hint
Think about how to access values inside nested dictionaries using keys.
โ“ Predict Output
intermediate
2:00remaining
What happens when accessing a missing key directly?
What error will this code produce?
info = {'city': 'Paris', 'country': 'France'}
print(info['population'])
Python
info = {'city': 'Paris', 'country': 'France'}
print(info['population'])
AKeyError
BParis
CNone
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
What happens if you try to get a key that does not exist in a dictionary?
โ“ Predict Output
advanced
2:00remaining
What is the output when using the get() method with a default value?
Consider the dictionary settings = {'theme': 'dark', 'font': 'Arial'}. What does print(settings.get('color', 'blue')) output?
Python
settings = {'theme': 'dark', 'font': 'Arial'}
print(settings.get('color', 'blue'))
AKeyError
BNone
Ccolor
Dblue
Attempts:
2 left
๐Ÿ’ก Hint
The get() method returns the default value if the key is missing.
โ“ Predict Output
advanced
2:00remaining
What is the output when accessing a key with a mutable object as key?
What happens when running this code?
my_dict = {[1, 2]: 'value'}
Python
my_dict = {[1, 2]: 'value'}
ASyntaxError
BTypeError
CKeyError
D{[1, 2]: 'value'}
Attempts:
2 left
๐Ÿ’ก Hint
Dictionary keys must be immutable types.
๐Ÿง  Conceptual
expert
2:00remaining
How many items are in the dictionary after this code runs?
What is the number of items in result after executing this code?
keys = ['a', 'b', 'a', 'c']
values = [1, 2, 3, 4]
result = {k: v for k, v in zip(keys, values)}
A3
B1
C2
D4
Attempts:
2 left
๐Ÿ’ก Hint
Dictionary keys must be unique; later values overwrite earlier ones.