Wu Yuxiong - born naturally PYTHON3 development of learning: Dictionaries

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
dict1 = { 'abc': 456 }
dict2 = { 'abc': 123, 98.6: 37 }
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
 
print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])
dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
 
print ("dict['Alice']: ", dict['Alice'])
dict = { ' the Name ' : ' Runoob ' , ' Age ' :. 7, ' Class ' : ' First ' } 
 
dict [ ' Age ' ] =. 8                # Update Age 
dict [ ' School ' ] = " novice Tutorial "   # add information 
 
 
Print ( " dict [ 'Age']: " , dict [ ' Age '])
print ("dict['School']: ", dict['School'])
= {dict ' the Name ' : ' Runoob ' , ' Age ' :. 7, ' Class ' : ' First ' } 
 
del dict [ ' the Name ' ] # delete key 'the Name' 
dict.clear ()      # empty dictionary 
del dict          # deleted Dictionary 
 
Print ( " dict [ 'Age']: " , dict [ ' Age ' ])
 Print ( " dict [ 'School']: ", dict['School'])
dict = {'Name': 'Runoob', 'Age': 7, 'Name': '小菜鸟'}
 
print ("dict['Name']: ", dict['Name'])
dict = {['Name']: 'Runoob', 'Age': 7}
 
print ("dict['Name']: ", dict['Name'])
= {dict ' the Name ' : ' Zara ' , ' Age ' :. 7 } 

Print ( " Dictionary length: D% " %   len (dict)) 
dict.clear () 
Print ( " after deleting dictionary length: D% " % len (dict))
= {dict1 ' the Name ' : ' Runoob ' , ' Age ' :. 7, ' Class ' : ' First ' } 
 
dict2 = dict1.copy ()
 Print ( " new dictionary is copied: " , dict2)
= {dict1 ' User ' : ' runoob ' , ' NUM ' : [l, 2,3 ]} 
 
dict2 = dict1           # shallow copy: reference object 
dict3 dict1.copy = ()    # shallow copy: deep copy of the parent object (a directory), sub-objects (level two) do not copy, or reference 
 
# modify data data 
dict1 [ ' User ' ] = ' the root ' 
dict1 [ ' NUM ' ] .remove (. 1 ) 
 
# output 
Print (dict1)
 Print (dict2 )
 Print (dict3)
= SEQ ( ' name ' , ' Age ' , ' Sex ' ) 
 
dict = dict.fromkeys (SEQ)
 Print ( " new dictionary is:% S " %   STR (dict)) 
 
dict = dict.fromkeys (SEQ, 10 )
 Print ( " the new dictionary is:% S " % str (dict))
x = ('key1', 'key2', 'key3')
 
thisdict = dict.fromkeys(x)
 
print(thisdict)
dict = {'Name': 'Runoob', 'Age': 27}

print ("Age 值为 : %s" %  dict.get('Age'))
print ("Sex 值为 : %s" %  dict.get('Sex', "NA"))
dict = { ' the Name ' : ' Runoob ' , ' Age ' :. 7 } 
 
# detection key Age presence or absence of 
IF   ' Age '  in dict:
     Print ( " key Age present " )
 the else :
     Print ( " key Age absent " ) 
 
# detecting presence or absence of key Sex 
iF   ' Sex '  in dict:
     Print ( " key exists Sex " )
 the else :
    Print ( " key does not exist Sex " ) 
 
 
# Not in 
 
# detect whether there is key Age 
IF   ' Age '  Not  in dict:
     Print ( " key does not exist Age " )
 the else :
     Print ( " key is present Age " )
dict = {'Name': 'Runoob', 'Age': 7}
 
print ("Value : %s" %  dict.items())
= {dict ' the Name ' : ' Runoob ' , ' Age ' :. 7 } 
 
Print ( " Age key values:% S " % dict.setdefault ( ' Age ' , None))
 Print ( " Sex key values: S% " % dict.setdefault ( ' Sex ' , None))
 Print ( " new Dictionary as: " , dict)
dict = {'Name': 'Runoob', 'Age': 7}
dict2 = {'Sex': 'female' }
 
dict.update(dict2)
print ("更新字典 dict : ", dict)
= {dict ' Sex ' : ' FEMALE ' , ' Age ' :. 7, ' the Name ' : ' Zara ' } 
 
Print ( " Dictionary all values: " , List (dict.values ()))
= {Site ' name ' : ' novice tutorial ' , ' ALEXA ' : 10000, ' URL ' : ' www.runoob.com ' } 
pop_obj = site.popitem ()
 Print (pop_obj)   
 Print (Site)

 

Guess you like

Origin www.cnblogs.com/tszr/p/10963225.html