Skip to content

Commit 0ef9c6f

Browse files
committed
Day 7 - Dictionaries & Sets
1 parent e5b5118 commit 0ef9c6f

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

Day 7 - Dictionaries & Sets/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Day 7: Dictionaries & Sets
2+
3+
## What You'll Learn:
4+
- Dictionary creation and key-value operations
5+
- Common dictionary methods (get(), keys(), items())
6+
- Set creation and mathematical operations
7+
- Unique element handling with sets
8+
9+
## Files:
10+
1. `dictionary_basics.py` - Core dictionary operations
11+
2. `set_operations.py` - Set methods and math operations
12+
3. `nested_data.py` - Combining dictionaries and sets
13+
14+
## Exercises:
15+
1. Create a user profile system that:
16+
- Stores user info as dict (name, email, preferences)
17+
- Allows updating preferences (add/remove items)
18+
- Checks if mandatory fields exist
19+
20+
2. Build a common interests finder that:
21+
- Takes 2 sets of hobbies/interests
22+
- Identifies shared interests
23+
- Shows unique interests per person
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Dictionary creation
2+
student = {
3+
"name": "Alice",
4+
"age": 20,
5+
"courses": ["Math", "CS"]
6+
}
7+
8+
# Accessing values
9+
print(student["name"])
10+
print(student.get("grade", "N/A")) # Safe access
11+
12+
# Modifying dict
13+
student["email"] = "[email protected]"
14+
del student["age"]
15+
16+
# Dictionary methods
17+
print("Keys:", student.keys())
18+
print("Items:", student.items())
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Dictionary with set values
2+
movie_ratings = {
3+
"action": {"John", "Mike", "Sarah"},
4+
"comedy": {"Mike", "Emma"},
5+
"drama": {"Sarah", "Emma"}
6+
}
7+
8+
# Add new voter to genre
9+
movie_ratings["action"].add("Emma")
10+
11+
# Find voters of multiple genres
12+
action_fans = movie_ratings["action"]
13+
drama_fans = movie_ratings["drama"]
14+
both_genres = action_fans & drama_fans
15+
16+
print(f"Fans of both action and drama: {both_genres}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Set creation
2+
primes = {2, 3, 5, 7, 11}
3+
evens = {2, 4, 6, 8}
4+
5+
# Set methods
6+
primes.add(13)
7+
primes.discard(2)
8+
9+
# Mathematical operations
10+
print("Union:", primes | evens)
11+
print("Intersection:", primes & evens)
12+
print("Difference:", primes - evens)
13+
14+
# Membership test
15+
print(7 in primes) # True

0 commit comments

Comments
 (0)