First, the definition method of the dictionary:
1、dic = {'name':'Karen','age':22,'hobby':'girl','is_handsome':True}
print(dic) #==>{'name':'Karen'}
dic = {'name':'Karen','age':22,'hobby':{‘name’:'xu','age':22},'is_handsome':True}
2、dic=dict((('name','Karen'),)) || dic=dict((['name','Karen'],)) || dic=dict([['name','Karen'],]) || dic=dict([('name','Karen'),])
print(dic) #==>{'name':'Karen'}
Two major characteristics of dictionaries: disorder, unique keys
The keys of the dictionary can only store immutable elements >>>>>Immutable types: integers, strings, tuples Variable types: lists, dictionaries
2. Increase
1、 dic1={'name':'Karen'}
dic1['age']=18
print(dic1) #==>{'name':'Karen','age':18}
2. dic1.sendefault('age',22) #If there is this key-value pair, do nothing and return the value, if not, increase and return the value
print(dic1)
3. Find
1、 dic={'name':'Karen','age':18}
print(dic['name']) #==>Karen
2. print(dic.keys()) #==> only print keys
The type is of type dict_keys, not a list
print(dic.values()) #==> just print the values
print(dic.items()) #==> print key-value pairs
Fourth, change
1、 dic={'name':'Karen','age':18}
dic['age']=33
print(dic) #==>{'name':'Karen','age':33}
2、 dic={'name':'Karen','age':18}
dic1={'a':'aaa','b':'bbb'}
dic.update(dic1) #replace if there is a value
5. Delete
1、 dic={'name':'Karen','age':18}
del dic('name')
print(dic) #==>dic={'age':18} delete the entire key-value pair
2、 dic.clear()
print(dic) #==>dic={}
3、 dic.pop('age')
print(dic) #==>dic={'age':18} deletes the entire key-value pair and returns the value as the return value
4 、 a = dic.popitem ()
print(a,dic) #randomly delete
6. Other operations and methods involved
1、dic=dict.fromkeys(['host1','host2','host3'],'test')
print(dic) #==>{'host1','host2','host3','test'} initialize a dictionary with keys, no values
2、dec=[5:'555',2:'666',4:'444']
print(sorted(dic.items()))
print(sorted(dic.keys()))
print(sorted(dic.values()))
3、dic={'name':'Karen','age':18}
for i in dic:
print(i,dic[i])