#__author: "Feng Lin"#DATE:2018/8/25#Increase:dict1={'name':'Lin',' Age': 21,'Sex':'Mans'}#Add with Keydict1['hight']=185Print(Dict1)#no keys are covereddict1[' Age']=24Print(Dict1)#presence key, no change, no addDict1.setdefault ('Weight')Print(Dict1) Dict1.setdefault ('Weight', 150) Dict1.setdefault ('Fat', 150)Print(Dict1)#Delete, with return value, can be set by defaultPrint(Dict1.pop ('Lalal','no This key'))#Delete return key valuePrint(Dict1.pop ('name','no This key'))#randomly deletes a key-value pair, returning a tuple of the deleted key-value pairsPrint(Dict1.popitem ())Print(Dict1)#do not use Del to delete dictionary key value pairs, if the key does not exist will be an error#del dict1[' age ']#del dict1[' Age1 ']#Empty Dictionarydict1.clear ()Print(Dict1)#Change Updatedict2={"name":"ou"," Age": 21}Print(dict2) dict3={"hight": 187,"Weight": 150}dict2.update (DICT3)Print(DICT3)Print(DICT2)#CheckPrint(Dict2.keys (), type (Dict1.keys ()))Print(Dict2.values ())Print(Dict2.items ())#Nestingdict4={ "name":"Sxixi", " Time": 1324, "Dict5":{" Age": 18,"Sex":"female"}, "DD":"XCCD"}dict4["Dict5"]["name"]="Lisha"Print(DICT4) dict4["Dict5"]["Sex"]="male"Print(DICT4)#Usea,b=[1,2]Print(A, b) A, B= ()Print(A, b)#returns the form of a tuple forIinchDict2.items ():Print(i)#This return can be a form that is not a tuple forKvinchDict2.items ():Print(k,v)# Precautions#Get Dictionary key value if no this key will error, at this time with get method, get not found will return none, avoid errorPrint(Dict2.get ("name1"))
Python Basic Learning-dictionaries