The dictionary is represented by curly braces {}, which is the most flexible built-in data type in Python. It is an unordered collection that accesses values by key and cannot be indexed.
Creation and use of dictionaries
The composition of a dictionary: A dictionary consists of curly braces {} to contain its data, the curly braces contain the key and its corresponding value, and a pair of keys and values becomes an item. Keys and values are separated by a colon: a comma, a number between items and items. An empty dictionary is a curly brace that does not contain any items, such as {}, which is an empty dictionary.
For example: {' name ': ' Wangyao ', ' Age ': 18}, {' Host ': ' HH ', ' Port ': 80}
Note: The key to the dictionary must be an immutable data type, and if you use tuples for keys, you must ensure that the pairs in the tuple are immutable types as well. mutable data types cannot be keyed.
Accessing values in a dictionary
Dictionaries are unordered and cannot be looked up in the same way as index subscripts. It has its own unique approach, which is to find it by key.
>>> variable name [key name]
Value
Add to
Dictionaries are added in different ways: Dictionary variable name [newly added key name] = value corresponding to new key
Modify
Dictionary modification format: dictionary variable name [to modify the value corresponding to the key name] = new value
Delete
There are three common ways to delete a dictionary, and the function is different. The following is a brief introduction to the format of these methods, the specific role and skills of the method in-depth study will be introduced.
1, del method: delete the corresponding value of the key, Del space variable name [key name], if the only write variable name is to delete this dictionary
2, Clear method: Empty the dictionary content, variable name. Clear ()
3, Python pop method: Delete the value corresponding to the key, but it will be the value of the output should be deleted after the
PS: Get all keys (key) with the keys () function, get all values (values) with the values () function, get all the key-value pairs with the items () function
You can also use for to traverse the keys or values inside the
Python Dictionary dict Operation definition