Skip to content

Instantly share code, notes, and snippets.

@aminsource
Created February 11, 2021 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminsource/59d3fb3116d11bb4b11a482f11eeb99f to your computer and use it in GitHub Desktop.
Save aminsource/59d3fb3116d11bb4b11a482f11eeb99f to your computer and use it in GitHub Desktop.
The get() method on Python dicts and its "default" arg
# 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