by Albert Major
(recap)
A dictionary is a collection of key-value pairs.
Similar to lists, but instead of using index numbers, it uses keys.
Example:
my_dict = {"apple": 4, "pear": 5, "banana": 7}
Keys can be any immutable type:
Values can be any data type.
Use square brackets to get a value:
print(my_dict["apple"]) # Output: 4
If a key doesn’t exist, Python raises a KeyError:
print(my_dict["orange"]) # KeyError
Adding a new key-value pair:
my_dict["orange"] = 66
Updating an existing value:
my_dict["orange"] = 55 # Now the value is updated to 55
Keys must be unique—you cannot have duplicate keys.