What is the purpose of the second parameter of the get() method for Python dictionaries? code example
Example 1: How are Python dictionaries different from Python lists?
list1 = ["a", "b" ,"c"]
dictionary1 = {"a":1, "b":2, "c":3}
Example 2: get function in dictionary
print('Salary: ', person.get('salary', 0.0))
Example 3: get() python
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!"
'''When "get()" is called it checks if the given key exists in the dict.
If it does exist, the value for that key is returned.
If it does not exist then the value of the default argument is returned instead.
'''