This multiple-choice question tests your understanding of the Python dictionary and the get() method.
What will be the output of the following code:
my_dict = {
'name': 'John',
'age': 25,
'city': 'New York'
}
result = my_dict.get('gender', 'Not specified')
print(result)
Select an option:🤔🧐
A: is the correct answer.
In the given code, the get()
method is used to retrieve the value of the key ‘gender’ from the dictionary my_dict
.
If the key is not found in the dictionary, the get()
method returns the default value specified as the second argument. In this case, ‘Not specified’ is provided as the default value.
Since ‘gender’ is not a key in the dictionary my_dict
, the get()
method returns the default value ‘Not specified’.