Python Programming - Variables - Discussion

Discussion Forum : Variables - General Questions (Q.No. 35)
35.
What is the output of the following code:
my_dict = {"a": 1, "b": 2}
del my_dict["c"]
print(my_dict)
{"a": 1, "b": 2}
{"a": 1, "b": 2, "c": None}
{"a": 1, "b": 2, "c": undefined}
An error is raised
Answer: Option
Explanation:

The given code attempts to delete the key "c" from the dictionary my_dict.

However, since "c" is not a valid key in the dictionary, the del my_dict["c"] operation will raise a KeyError.

Therefore, the output of the code will be a KeyError:

Traceback (most recent call last):
  File "example.py", line 2, in 
    del my_dict["c"]
KeyError: 'c'
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.