Module 4: Data Collections and Functions
Dictionaries
Dictionaries are unordered collections of key-value pairs. They are mutable.
- Creating:
my_dict = {"name": "Alice", "age": 30, "city": "New York"}
ormy_dict = dict(name="Alice", age=30)
- Accessing: Use keys.
my_dict["age"]
is30
. - Modifying/Adding:
my_dict["age"] = 31
,my_dict["country"] = "USA"
- Common Methods:
keys()
,values()
,items()
,get(key, default_value_optional)
,pop(key)
. Keys must be unique and immutable (e.g., strings, numbers, tuples).