CBSE Class 12 Computer Science
Question 71 of 105
Python Revision Tour II — Question 20
Back to all questions- In this example, the dictionary D does not contain the key 'd'. Therefore, attempting to access this key by D['d'] results in a KeyError because the key does not exist in the dictionary.
- If we try to assign a value to a nonexistent key in a dictionary, python will create that key-value pair in the dictionary. In this example, the key 'd' did not previously exist in the dictionary D. When we attempted to assign the value 'spam' to the key 'd', python created a new key-value pair 'd': 'spam' in the dictionary D.
D = {'a' : 1, 'b' : 2, 'c' : 3}
D['d'] = 'spam'D = {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 'spam'}