1. Dictionary
- The dictionary is a mutable collection composed of key-value pairs.
- Key is a unique identifier, and Value is data associated with the key.
1-1. Creation
Dictionaries are created using curly braces {}, and key-value pairs are separated by commas ,. Each key-value pair is separated by a colon :.
1-2. Mutability
Dictionaries are mutable. That is, the user can add, remove or change existing value.
- Add a key-value pair: dict[key] = value
- Remove a key-value pair: del dict[key]
- Modify a value: dict[key] = new_value
1-3. Key and Value Constraints
The keys of the dictionary must be immutable. For example, strings, integers, and tuples can be used as dictionary keys, but lists cannot be used as dictionary keys. However, the value of the dictionary can be of any type.
1-4. Functions and Methods
Dictionaries have several functions and methods:
- len(dict): Returns the size of the dictionary (number of key-value pairs).
- dict.keys(): Returns an iterator over all the keys in the dictionary.
- dict.values(): Returns an iterator over all the values in the dictionary.
- dict.items(): Returns an iterator over key-value pair tuples.
- dict.get(키, 기본값): Returns the value associated with the key. If the key does not exist, returns the default value.
- in: Checks if a key is in the dictionary.
1-5. Membership Test
The in operator can be used to check if a specific key is in a Dictionary.
'Python(Eng. ver)' 카테고리의 다른 글
9. Conditional Statements in Python (0) | 2024.03.14 |
---|---|
8. Operator in Python (0) | 2024.03.14 |
6. Python collection type - Set (0) | 2024.03.14 |
5. Python collection type - Tuple (0) | 2024.03.14 |
4. Python collection type - List (0) | 2024.03.14 |