Defined
Using key-value pairs,
>>> person = {"name":"Keven"," Age": 15,"Gender":"male"}>>> person['name']'Keven'>>>type (person)<type'Dict'>
A dictionary can be modified in place, that is, it is mutable.
>>> dict1={}>>> ID (dict1)139736380198256>>> dict1["name "keven">>> ID (dict1)139736380198256>>> dict1{'name' 'keven'}
Using tuples to construct dictionaries, the method is as follows:
>>> name = ([" First","Google"],["Second","Yahoo"])>>> website =dict (name)>>>website{'Second':'Yahoo',' First':'Google'}
Or in such a way:
>>> ad = dict (name="keven", age=42)>>> ad{' Age ' ' name ' ' Keven '
Basic operations
Len (d), returns the number of key-value pairs in the dictionary (d)
>>> City_code = {'Suzhou':'0512','Beijing':'011','Shanghai':'012','Tangshan':'0315'}>>>Len (city_code)4
D[key], returns the value of the key (key) in the dictionary (d)
>>> city_code["Suzhou"]'0512'
D[key]=value, assigns the value to the key (key) in the dictionary (d)
>>> City_code = {'Suzhou':'0512','Beijing':'011','Shanghai':'012','Tangshan':'0315'}>>> city_code["Shenzhen"]="1212">>>city_code{'Suzhou':'0512','Beijing':'011','Shanghai':'012','Tangshan':'0315','Shenzhen':'1212'}
• Del D[key], delete the key (key) entry for dictionary (d) (delete the key value pair)
>>>delcity_code["Shanghai"]>>>city_code{'Suzhou':'0512','Beijing':'011','Tangshan':'0315','Shenzhen':'1212'}
key in D, check if the dictionary (d) contains keys key
" Shenzhen " inch city_codetrue " sz " inch City_codefalse
Dictionaries can also implement formatted characters
>>> City_code = {"Suzhou":"0512","Tangshan":"0315","Hangzhou":"0571"}>>>"Suzhou is a beautiful city, it area code is% (Suzhou) s"%City_code'Suzhou is a beautiful city, it area code is 0512
Python Learning note 6-dictionaries