Skip to content

Instantly share code, notes, and snippets.

@penn201500
Created August 29, 2017 10:10
Show Gist options
  • Save penn201500/a65bac55352434efde5aac41d40bce91 to your computer and use it in GitHub Desktop.
Save penn201500/a65bac55352434efde5aac41d40bce91 to your computer and use it in GitHub Desktop.
python dict get method
# 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