Python dictionary (Dictionary) SetDefault () method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.