Skip to content

Instantly share code, notes, and snippets.

@Yunkou
Created August 30, 2017 18:13
Show Gist options
  • Save Yunkou/93dad5d75d512d2d5f5c3a3a1ec02f6e to your computer and use it in GitHub Desktop.
Save Yunkou/93dad5d75d512d2d5f5c3a3a1ec02f6e to your computer and use it in GitHub Desktop.
Default dict values
ages = {
'Mary': 31,
'Jonathan': 28
}
# The bad way
if 'Dick' in ages:
age = ages['Dick']
else:
age = 'Unknown'
print('Dick is %s years old' % age)
# The good way
age = ages.get('Dick', 'Unknown')
print('Dick is %s years old' % age)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment