Python dictionary

Python dictionary

1. Definition dictionary
Insert picture description here

2. Form: key-value (key-value)
keywords must be strings
Insert picture description here
3. List to dictionary
Insert picture description here
Insert picture description here
4. Addition
, deletion, modification, and query of dictionary The addition , deletion, modification, and query of the list are all performed on the following.
The dictionary is carried out through the key, because the key is unique in the dictionary.

4.1, add
Insert picture description here
Insert picture description here
4.2, change
Insert picture description here
Insert picture description here
4.3, check
Insert picture description here
4.4, delete

Insert picture description here

5. The built-in function in the dictionary is to use the dictionary to call the function
Insert picture description here

5.1. items()
Insert picture description here
Insert picture description here
5.2 values()
take out all the values ​​in the dictionary and save them in the list
Insert picture description here
Insert picture description here
5.3 get() returns value

get(key) ------>value If the value cannot be obtained, no error will be reported, and None will be returned.
get(key, default) ------>value If the value can be obtained, the value in the dictionary will be returned , If not available, it will return the value of default
Insert picture description here
Insert picture description here
5.4, ​​pop(key) will return the value corresponding to the key that was successfully deleted
popitem() generally delete the elements of the dictionary one by one from the end
Insert picture description here
Insert picture description here

5.5. update()
merges the addition with the new, which is equivalent to the connection of the list []+[], but the dictionary does not have a +. Use update() to merge the two dictionaries
Insert picture description here
Insert picture description here
5.6, fromkeys(seq) to
combine the sequence seq (such as a list or a character) String) into the form of a dictionary, if no value is specified, use None
Insert picture description here
Insert picture description here

6. Case
User registration function: username, password, Email, phone
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44994799/article/details/109682283