Describe
The Python Dictionary (Dictionary) SetDefault () function is similar to the Get () method, and if the key does not already exist in the dictionary, the key is added and the value is set to the default value.
Grammar
SetDefault () method syntax:
Dict. SetDefault(key,default=None)
Parameters
- Key--The value of the lookup.
- Default--When the key does not exist, set the defaults for the key, and the presence is not set.
return value
The method does not have any return value.
Instance
The following example shows how the SetDefault () function is used:
#!/usr/bin/pythondict = { ' Name ' : ' Zara ' , ' age ' : 7
Dict_default = dict setdefault ( ' Sex ' , Man ')
print "Value:%s" % Dict. ( ' age ' , print "Value:%s" % Dict. ( ' Sex ' , ' man ' ) /span>
The result of the above example output is:
Value:7value:None
>>>dict_default
>>>man
>>>dict =
{' Name ':' Zara ',' age ':7, ' Sex ': ' Mans '}
Python dictionary (Dictionary) SetDefault () method