1# The get() method on dicts
2# and its "default" argument
3
4name_for_userid = {
5 382: "Alice",
6 590: "Bob",
7 951: "Dilbert",
8}
9
10def greeting(userid):
11 return "Hi %s!" % name_for_userid.get(userid, "there")
12
13>>> greeting(382)
14"Hi Alice!"
15
16>>> greeting(333333)
17"Hi there!"
18
19'''When "get()" is called it checks if the given key exists in the dict.
20
21If it does exist, the value for that key is returned.
22
23If it does not exist then the value of the default argument is returned instead.
24'''
25# transferred by @ebdeuslave
26# From Dan Bader - realpython.com