Advertisements
Advertisements
Question
If my_dict is a dictionary as defined below, then which of the following statements will raise an exception?
my_dict = {'apple': 10, 'banana': 20, 'orange': 30}
Options
my_dict.get('orange')
print(my_dict['apple', 'banana'])
my_dict['apple']=20
print(str(my_dict))
MCQ
Solution
print(my_dict['apple', 'banana'])
Explanation:
The code attempts to access a dictionary key via a tuple ('apple', 'banana') rather than separate keys. This key doesn't exist, resulting in a KeyError.
shaalaa.com
Is there an error in this question or solution?