Dictionaries - Your Personal Information Organizer

A dictionary stores data in key-value pairs. Think of it as a real dictionary where you look up a word (key) to find its meaning (value). Keys must be unique and are used to quickly find values. Dictionaries are unordered (order doesn't matter) and very fast for looking things up.

# Creating and using dictionaries
# Example 1: Creating a dictionary
student = {
    "name": "Alex",
    "age": 20,
    "major": "Computer Science",
    "gpa": 3.8
}
print("Student information:")
print(student)

# Accessing values
print("\nAccessing specific information:")
print("Name:", student["name"])
print("Age:", student["age"])

# Example 2: Adding and modifying
student["year"] = "Sophomore"  # Adding new key-value pair
student["gpa"] = 3.9  # Modifying existing value
print("\nUpdated student:")
print(student)

# Example 3: Checking if key exists
if "major" in student:
    print(f"\nMajor: {student['major']}")

# Example 4: Getting all keys and values
print("\nAll keys:", student.keys())
print("All values:", student.values())

# Example 5: Looping through dictionary
print("\nStudent details:")
for key, value in student.items():
    print(f"{key}: {value}")

# Example 6: Phone book
def find_contact(phone_book, name):
    if name in phone_book:
        return phone_book[name]
    else:
        return "Contact not found"

contacts = {
    "Alice": "555-1234",
    "Bob": "555-5678",
    "Charlie": "555-9012"
}

print("\nPhone Book:")
for name, number in contacts.items():
    print(f"{name}: {number}")

print("\nFinding Alice's number:", find_contact(contacts, "Alice"))