Welcome back! Yesterday, we learned about lists and list comprehensions in Python. Today, we’ll dive into dictionaries and sets, which are powerful data structures for storing and managing collections of data. By the end of this day, you’ll know how to create and use dictionaries and sets effectively. Let’s get started!
What are Dictionaries?
Dictionaries are collections of key-value pairs. They are unordered, mutable, and indexed by unique keys.
Creating Dictionaries
You can create a dictionary by placing comma-separated key-value pairs inside curly braces:
student = {
"name": "Andrew",
"age": 14,
"grade": "9th"
}
Accessing Dictionary Elements
You can access dictionary elements using their keys:
print(student["name"]) # Output: Andrew
print(student["age"]) # Output: 14
Modifying Dictionaries
You can add, update, and remove key-value pairs in a dictionary:
# Adding a new key-value pair
student["school"] = "High School"
print(student)
# Updating an existing value
student["grade"] = "10th"
print(student)
# Removing a key-value pair
del student["age"]
print(student)
Dictionary Methods
Dictionaries come with several useful methods:
keys(): Returns a list of keys
values(): Returns a list of values
items(): Returns a list of key-value pairs
Example:
print(student.keys()) # Output: dict_keys(['name', 'grade', 'school'])
print(student.values()) # Output: dict_values(['Andrew', '10th', 'High School'])
print(student.items()) # Output: dict_items([('name', 'Andrew'), ('grade', '10th'), ('school', 'High School')])
What are Sets?
Sets are collections of unique items. They are unordered and mutable.
Creating Sets
You can create a set by placing comma-separated values inside curly braces or by using the set() function:
fruits = {"apple", "banana", "cherry"}
numbers = set([1, 2, 3, 4, 5])
Adding and Removing Elements
You can add and remove elements from a set using the add() and remove() methods:
fruits.add("date")
print(fruits) # Output: {'date', 'apple', 'banana', 'cherry'}
fruits.remove("banana")
print(fruits) # Output: {'date', 'apple', 'cherry'}
Set Operations
Sets support various mathematical operations, such as union, intersection, difference, and symmetric difference:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Union
print(set1 | set2) # Output: {1, 2, 3, 4, 5, 6}
# Intersection
print(set1 & set2) # Output: {3, 4}
# Difference
print(set1 - set2) # Output: {1, 2}
# Symmetric Difference
print(set1 ^ set2) # Output: {1, 2, 5, 6}
Practice Time!
Let’s put what we’ve learned into practice. Write a Python program that creates, modifies, and manipulates dictionaries and sets.
# Creating a dictionary
car = {
"brand": "Toyota",
"model": "Camry",
"year": 2020
}
# Accessing dictionary elements
print(car["brand"]) # Output: Toyota
print(car["model"]) # Output: Camry
# Modifying the dictionary
car["year"] = 2021
car["color"] = "blue"
print(car) # Output: {'brand': 'Toyota', 'model': 'Camry', 'year': 2021, 'color': 'blue'}
# Using dictionary methods
print(car.keys()) # Output: dict_keys(['brand', 'model', 'year', 'color'])
print(car.values()) # Output: dict_values(['Toyota', 'Camry', 2021, 'blue'])
print(car.items()) # Output: dict_items([('brand', 'Toyota'), ('model', 'Camry'), ('year', 2021), ('color', 'blue')])
# Creating a set
animals = {"cat", "dog", "bird"}
# Adding and removing elements
animals.add("fish")
animals.remove("dog")
print(animals) # Output: {'cat', 'bird', 'fish'}
# Set operations
set_a = {1, 2, 3}
set_b = {3, 4, 5}
print(set_a | set_b) # Union: {1, 2, 3, 4, 5}
print(set_a & set_b) # Intersection: {3}
print(set_a - set_b) # Difference: {1, 2}
print(set_a ^ set_b) # Symmetric Difference: {1, 2, 4, 5}
Conclusion
Great job today! You’ve learned how to create and manipulate dictionaries and sets in Python, which are essential for managing collections of data. Tomorrow, we’ll dive into classes and objects, introducing you to object-oriented programming. Keep practicing and having fun coding!
Leave a Reply