Great question ๐ Let’s go through the code step by step:
Explanation:
-
Dictionary Creation
{"a": 1}
d = {"a": 1}
→ A dictionary is created with only one key-value pair: -
Accessing a Key
print(d["b"])
→ Python looks for the key "b" in the dictionary.
Since "b" does not exist, Python raises a KeyError. -
Exception Handling
print("missing key")
The try block has an except KeyError clause.
So instead of crashing, Python runs: -
Output
missing key
✅ Final Output:
missing key


0 Comments:
Post a Comment