Post

Python Programming
Python Programming@PythonPr·
Python Question / Quiz; What is the output of the following Python code, and why? Comment your answers below!
Python Programming tweet media
English
24
12
137
11.4K
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr Answer: B Solution: my_dict is a dict with 3 keys: 'a', 'b', and 'c'. Let's figure out what's: my_dict.get('d', 4) The `get` dictionary method is used to get value for a key from the dictionary. Similar to [] notation. But there is a difference. If you try to access +
English
1
0
2
452
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@PythonPr a non-existent key with [], you'll get an error. So, something like my_dict['d'] will give a KeyError, because 'd' isn't a key in my_dict. But sometimes, if a key isn't in the dictionary, we don't want an error. That's when `get` comes in to help... +
English
1
0
0
38
Partager