Module 5.3
Module 5.3
What is Dictionaries?
A dictionary is an Ordered(After 3.7) collection of key-value pairs.
Operations on Dictionaries:
1. Creating a Dictionary:
my_dict = {"name": "Alice", "age": 25, "job": "Engineer"}
print(my_dict) # Output: {'name': 'Alice', 'age': 25, 'job': 'Engineer'}
2. Accessing Values:
print(my_dict["name"]) # Output: Alice
3. Modifying Values:
my_dict["age"] = 26
print(my_dict) # Output: {'name': 'Alice', 'age': 26, 'job': 'Engineer'}
8. Dictionary Methods:
- keys(): Returns all the keys in the dictionary.
- values(): Returns all the values in the dictionary.
- items(): Returns all key-value pairs in the dictionary.
- get(): Retrieves the value for a given key.
- pop(): Removes a key-value pair by key.
Summary:
- Lists: Ordered, mutable, and can contain elements of various types. You can add, remove, and modify
items.
- Tuples: Ordered, immutable, and can also hold elements of various types. Once created, their content
cannot be changed.
- Dictionaries: Unordered, mutable, key-value pairs where keys are unique and immutable. You can modify,
add, and remove key-value pairs.