Assigning default values for get() function in dictionary

By | 14th June 2021

In the following code, we are assigning default values to the dictionary. First, we can create the dictionary and assigning the values to the names. Then, we are trying to fetch the dictionary value based on the key. If there is no such key, we will assign a default value.

dict_customer= {
    1: "Ram",
    2: "Vivek",
    3: "Ramya",

def getWelcomeMsg(custid):
    return "Hi %s!" % dict_customer.get(custid, "Guest")

Hi Ram!
Hi Guest!