Created
May 9, 2019 15:26
-
-
Save ferminhg/d78befd1ba3cd1311e05fa5b700a8bd4 to your computer and use it in GitHub Desktop.
The get() method on Python dicts and its "default" arg
This file contains hidden or 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