Dictionary type:
effect:
In the {} to store a plurality of values can be separated by a comma.
Access to key-value, high value of speed.
definition:
key must be immutable type, value may be any type
dict1=dict({'age':18,'name':'tank'})
Equivalent dict1 = { 'age': 18, 'name': 'tank'}
print(dict1) # {'age': 18, 'name': 'tank'}
print (type (dict1)) # <class 'dict'>
the priority control operations:
1. Press the key values: preferably be kept
# deposit a level: the value 9 to dict1
dict1 [ 'Level'] = 9
Print (dict1 ) {# 'Age': 18 is, 'name': 'Tank', 'Level':}. 9
Print (dict [ 'name' ]) #tank
# 3, membership operator in and not in only determination dictionary key, can not determine the value of
# Print ( 'name' in dict1) # True
# Print ( 'Tank' in dict1) # False
# Print ( 'Tank' not in dict1) # True
# 4, delete
Del dict1 # [ 'Level']
Print (dict1) {# 'Age': 18 is, 'name': 'Tank'}
. 5, button keys (), the value of values (), on the key-value items ()
# dictionary to give All key
Print (dict1.keys ())
# get all the values in the dictionary values
Print (dict1.values ())
to get all the items in the dictionary
Print (dict1.items ())
6 loop:
loop through all of the key values
for key in dict1:
Print (Key)
Print (dict1 [Key])
#get
= {dict1 'Age': 18 is, 'name': 'Tank'}
Print (dict1.get ( 'Age'))
Print (dict1 [ 'Age']) This is equivalent to two, but it is still a little difference the
[] values:
print(dict1['sex']) # KeyError: 'sex'
# Get the value
Print (dict1.get ( 'Sex')) # None
# if no sex, to set a default value
print (dict1.get ( 'sex', 'male'))