Python 0 Basics Beginner's Nine dictionary

Source: Internet
Author: User
1. Definition of a dictionary
A dictionary is characterized by a dictionary of symbols that are flower (large) brackets, in which the elements in the dictionary use key-value pairs. A key-value pair as a data item.
2. How to create a dictionary
(1) Use of the definition method

dict1={' Li Ning ': ' Everything is possible ', ' Nike ': ' Just do it ', ' Adidas ': ' Impossible is nothing ', ' Fish C Studio ': ' Programming makes the world a better place '}print ("Fish C Studio slogan is:", dict1[ "Fish C Studio") print ("\ n") Dict2={1: ' One ', 2: ' One ', ' 3: ' Three '} #可以使用键作为索引获得相应的值. Print (dict2[1]) print (' \ n ')

2. Use the Dict () function to create a dictionary
It is worth noting that Dict () has only one parameter

The #之所以会有那么多的括号 is because the Dict function has only one parameter dict3=dict ((' F ', ' n '), (' I ', ' '), (' s ', ' '), (' H ', ' 104 '), (' C ', ') ') print (' \ N ')

(3) Create a dictionary using the dict () + keyword parameter
Note: The keyword parameter does not have a quotation mark

Dict4=dict (Small turtle = "programming makes the world a better place", Cang jing empty = "Let av Conquer otaku") print (DICT4)

3. Modify the entries in the dictionary
(1) Modify the existing entries in the dictionary

dict4[' Cang jing empty ']= "AV is not the welfare of otaku" print (dict

(2) Modifying items that do not exist in the dictionary
This is special, not only the exception is not present, but the data item is inserted in the dictionary.

dict4[' Edison ']= "genius is 99% perspiration plus 1% inspiration" print (DICT4)

4. Built-in functions for dictionaries
(1) Fromkeys (S[,v]) create a dictionary and return a dictionary, the second parameter defaults to
None.

Dict1={}dict1=dict1.fromkeys ((+)) print (Dict1) print ("\ n") Dict1=dict1.fromkeys ((+), (' numbers ')) print ( DICT1)

(2) keys () function: Gets all the keys of the dictionary

Dict1=dict1.fromkeys (Range (32), likes) print (Dict1) for Eachkey in Dict1.keys ():     print (eachkey,end= ") print (" \ n ")

(3) VALUES () function: Gets all the values of the dictionary

#values () A collection of all values for Eachvalue in Dict1.values ():     print (eachvalue,end= "") print ("\ n")

(4) the items () function: Gets all the items of a function

For Eachitem in Dict1.items ():     print (eachitem,end= "")

(5) The Get () function gets the value corresponding to the dictionary key and returns none if it does not exist

Str1=dict1.get (+) print (STR1)

(6) In or not in member relations: Judging whether a key or value is in the dictionary

Print ("\ n") print ("\ n") print ("\ Dict1")

(7) Clear (): Clears all items in the dictionary

Dict1.clear () print (DICT1)

(8) Update () function: Update the contents of another dictionary with one dictionary

A={1: "One"}b={1: "One"}a.update (b) print (a)

And finally, the difference between using a "=" (an assignment symbol) and using a copy function:
In general, using an assignment symbol simply creates a label that points to the data, but it still has a share of the data, similar to the person's size name. Data that is completely non-relational when using a function.

Print ("\ n") list1=[' 1 ', ' 2 ', ' 3 ']list2=list1[:3]list3=list1print (ID (list1)) print (ID (list2)) print (ID (list3)) print ( LIST2) print (LIST3) list1[0]= "666" Print (list2) print (LIST3) #根据id可知, the assignment operation of the dictionary creates a label that points to the # data, not a true copy a={"name": "The Little Turtle "}b=aprint (ID (a)) print (ID (b)) print (b) #将a指向了空字典, instead of changing the size of its data # here is similar to the assignment of pointers, the assignment of pointers is to assign the value of the address # instead of the value of the Data a={} #空集类似于空指针print ( ID (a)) print (a) print (b) #赋值与全拷贝函数, the difference is similar to the above a={1: ' One ', 2: ' One ', 3: ' Three '}b=a.copy () C=aprint (ID (a)) print (ID (b)) print (ID (c)) print (a) print (b) print (c)

The knowledge of the dictionary ends here, if any more will be added in time.

The above is the Python 0 basic Primer Nine dictionary content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.