Created
August 30, 2017 18:13
-
-
Save Yunkou/93dad5d75d512d2d5f5c3a3a1ec02f6e to your computer and use it in GitHub Desktop.
Default dict values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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