Explanation:
1. Creating a Dictionary
d = {"a": 1}
Here, a dictionary named d is created.
A dictionary in Python stores data in key–value pairs.
In this case:
Key: "a"
Value: 1
So the dictionary looks like:
{"a": 1}
2. Using the .get() Method
print(d.get("b", 2))
➤ What .get() does:
The .get() method is used to retrieve the value of a key from a dictionary.
Syntax:
dictionary.get(key, default_value)
➤ In this example:
"b" is the key we are trying to access.
2 is the default value.
3. Key Lookup Behavior
The dictionary d does NOT contain the key "b".
Instead of throwing an error, .get():
Returns the default value, which is 2.
4. Output
2

0 Comments:
Post a Comment