The Python programming language is a general computer language that is easy to learn. For beginners, some basic applications must be mastered first. For example, the operations related to the Python dictionary we introduced to you today are the skills we need to master in the learning process.
Python Dictionary (Dictionary) is a data type of the ing structure, which consists of unordered "key-value pairs. The dictionary key must be of unchangeable type, such as string, number, and tuple. The value can be of any Python data type.
1. Create a Python dictionary
- >>> Dict1 ={}# create an empty dictionary
- >>> Type (dict1)
- <Type 'dict '>
2. Add Python dictionary elements: Two Methods
- >>> Dict1 ['a'] = 1 # first
- >>> Dict1
- {'A': 1}
- # Method 2: setdefault
- >>> Dict1.setdefault ('B', 2)
- 2
- >>> Dict1
- {'A': 1, 'B': 2}
3. Delete the Python dictionary
- # Deleting a specified key-Value Pair
- >>> Dict1
- {'A': 1, 'B': 2}
- >>> Del dict1 ['a'] # You can also use the pop method, dict1.pop ('A ')
- >>> Dict1
- {'B': 2}
- # Clearing the dictionary
- >>> Dict1.clear ()
- >>> Dict1 # The dictionary is empty.
- {}
- # Deleting dictionary objects
- >>> Del dict1
- >>> Dict1
- Traceback (most recent call last ):
- File "<interactive input>", line 1, in <module>
- NameError: name 'dict1' is not defined
The above is an introduction to the Python dictionary.