{"id":892,"date":"2021-06-14T13:43:15","date_gmt":"2021-06-14T08:13:15","guid":{"rendered":"https:\/\/techieshouts.com\/?p=892"},"modified":"2022-08-09T19:04:20","modified_gmt":"2022-08-09T13:34:20","slug":"assigning-default-values-for-get-function-in-dictionary","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/assigning-default-values-for-get-function-in-dictionary\/","title":{"rendered":"Assigning default values for get() function in dictionary"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"beyond\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">dict_customer= {\n    1: \"Ram\",\n    2: \"Vivek\",\n    3: \"Ramya\",\n}\n\ndef getWelcomeMsg(custid):\n    return \"Hi %s!\" % dict_customer.get(custid, \"Guest\")\n\nprint(getWelcomeMsg(1))\nprint(getWelcomeMsg(45))<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Output:\nHi Ram!\nHi Guest!<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,14],"tags":[121,123,122],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/892"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=892"}],"version-history":[{"count":3,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/892\/revisions"}],"predecessor-version":[{"id":922,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/892\/revisions\/922"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}