Python Basic Learning-dictionaries

Source: Internet
Author: User

#__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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.