dictionary {}
Try it yourself →Stores key-value pairs.
Syntax
name = {"key": value}Description
A dictionary is created with curly braces. A value is accessed via its key (not an index). It's like 'labeled boxes' in the real world.
Returns
A dictionary object.
Example
person = {"name": "Alex", "age": 36}
print(person["name"])
person["city"] = "London"
print(person)Expected output
Alex
{'name': 'Alex', 'age': 36, 'city': 'London'}When to use?
Holding an object's attributes (a user, a product); JSON data.
Common mistakes
- ⚠️ Accessing a missing key raises an error; use .get() for safe access.