If you want to retrieve the key's value if it exists, you can also use
try:
value = a[key]
except KeyError:
# Key is not present
pass
If you want to retrieve a default value when the key does not exist, use
value = a.get(key, default_value)
.
If you want to set the default value at the same time in case the key does not exist, use
value = a.setdefault(key, default_value)
.