Dictionaries


Think of dictionaries as treasure maps for your data. In Python, a dictionary is a versatile collection that stores data in key-value pairs, allowing you to organize and access information efficiently.

Creating Dictionaries and Accessing Values:

Creating a dictionary involves pairing keys with values using curly braces. You can access values by referencing their keys.

Example: Creating Dictionaries and Accessing Values

person = {
    "name": "Alice",
    "age": 30,
    "city": "Wonderland"
}
print(person["name"])   # Output: Alice
print(person["age"])    # Output: 30

3. Modifying and Adding Entries:

Dictionaries are also dynamic so you can change values, add new entries, or remove existing ones.

Example: Modifying and Adding Entries

# Modifying an entry's value
person["age"] = 31

# Adding a new entry
person["job"] = "Adventurer"

Leave a Reply

Your email address will not be published. Required fields are marked *