Values in a dictionary can be referred to using the same ·[·]
operator as for indexing lists. For
example, {'one': 1, 'two': 2}['two']
is 2
, but {'one': 1, 'two': 2}['three']
will throw an exception (specifically, a KeyError
) because there is no key 'three'
in the
dictionary.
If the possible absence of a key is okay, the get
method can be used instead. Called with one argument,
a key, the get
method normally returns the value corresponding to that key, but it will instead return
None
, a special value built into Python, if there is no such value—it will never throw an exception.
Called with two arguments, a key and a default value, the get
method works similarly, but returns the
default value in place of None
. For instance, {'one': 1, 'two': 2}.get('two')
is
2
, {'one': 1, 'two': 2}.get('three')
is None
, and
{'one': 1, 'two': 2}.get('three', 100)
is 100
.
Fill in the placeholders so that the actual outputs match the expected outputs.