python dictionary additions and deletions

2010-10-01 Today is National Day mood, learning journey to open the code.. 
Hey, very white dish.
Parts of the code today:

. 1 xiaoming_dict = { " name " : " Bob " }
 2  
. 3  # values 
. 4  Print (xiaoming_dict [ " name " ])
 . 5  # when the value, if the key does not exist, the program will be given 
. 6  # Print (xiaoming_dict [ " name12 "]) 
. 7  
. 8  # add / modify 
9  #   If key does not exist, will be added to the key 
10 xiaoming_dict [ " Age " ] = 26 is
 . 11 xiaoming_dict [ " Gender " ] = " MALE" 
12  # If the key is present, will modify the existing key-value pair 
13 is xiaoming_dict [ " name " ] = " hard " 
14  Print (xiaoming_dict)
 15  
16  # deleted 
. 17 xiaoming_dict.pop ( " Gender " )
 18 is  # deleted does not exist key-value pairs, the program will be given 
19  # xiaoming_dict.pop ( "gender12") 
20  Print (xiaoming_dict)

Run the code encountered some problems, such as key-value pairs to increase my time, every time to direct a new dictionary content to be replaced, do not know. Until pop, reported KeyError, can not find the corresponding key right? turned out to be
xiaoming_dict [ "age"] = 26 I wrote these xiaoming_dict = [ "age", 26 ]. 
programmer or road is long Come, happiness and earth.

Guess you like

Origin www.cnblogs.com/ddxmt/p/11615741.html