Last active
November 10, 2018 09:40
-
-
Save etigui/a28e2140086c4e2e10e0e5ae52644288 to your computer and use it in GitHub Desktop.
Python default argument return when "get()" is called if the given key not exists in the dict (PyTricks)
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
# The get() method on dicts | |
# and its "default" argument | |
name_for_userid = { | |
382: "Alice", | |
590: "Bob", | |
951: "Dilbert", | |
} | |
def greeting(userid): | |
return "Hi %s!" % name_for_userid.get(userid, "there") | |
>>> greeting(382) | |
"Hi Alice!" | |
>>> greeting(333333) | |
"Hi there!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment