This tests the dictionary .get() method.
Code:
Step 1: Recall .get(key, default)
dict.get(key, default) tries to fetch the value for key.
-
If the key exists, it returns its value.
-
If the key doesn’t exist, it returns the default value (or None if no default is given).
Step 2: Apply to this example
-
Dictionary is:
{"a":1, "b":2} -
Key "c" is not present.
-
A default value 99 is provided.
So:
d.get("c", 99) → 99Final Output:
99✅ Explanation:
.get("c", 99) safely checks "c". Since "c" is missing, it returns the default 99 instead of raising a KeyError.


0 Comments:
Post a Comment