Skip to content

Commit 2c7442b

Browse files
updated
1 parent 1b1c8b4 commit 2c7442b

File tree

34 files changed

+21974
-0
lines changed

34 files changed

+21974
-0
lines changed

21-sets/README.md

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
# Chapter 21: Sets 🎲
2+
3+
## What You'll Learn
4+
Master Python sets, an unordered collection type that automatically handles unique values and provides powerful mathematical set operations.
5+
6+
## Learning Objectives
7+
By the end of this chapter, you will:
8+
- Understand what sets are and when to use them
9+
- Create and manipulate sets using methods
10+
- Leverage automatic duplicate removal
11+
- Apply set operations (union, intersection, difference)
12+
- Choose between lists, sets, and tuples appropriately
13+
- Optimize code using set membership testing
14+
15+
## Concept Explanation
16+
17+
### What is a Set?
18+
A **set** is an unordered collection of unique items. Think of it like a bag of items where:
19+
- No duplicates allowed
20+
- No specific order (can't access by index)
21+
- Items are immutable (but you can add/remove from set)
22+
- Very fast membership testing
23+
24+
### Collection Types Comparison
25+
| Type | Symbol | Ordered | Changeable | Duplicates | Speed |
26+
|------|--------|---------|------------|------------|-------|
27+
| List | `[]` | ✅ Yes | ✅ Yes | ✅ Yes | Medium |
28+
| Set | `{}` | ❌ No | ✅ Yes* | ❌ No | Fast |
29+
| Tuple | `()` | ✅ Yes | ❌ No | ✅ Yes | Fastest |
30+
31+
*Can add/remove items, but items themselves must be immutable
32+
33+
### Set Syntax
34+
```python
35+
# Creating sets
36+
empty_set = set() # Must use set(), not {}
37+
fruits = {"apple", "banana", "cherry"}
38+
numbers = {1, 2, 3, 4, 5}
39+
mixed = {1, "hello", 3.14} # Different types OK
40+
```
41+
42+
### Why Use Sets?
43+
1. **Remove duplicates** automatically
44+
2. **Fast membership testing** (`in` operator)
45+
3. **Mathematical operations** (union, intersection)
46+
4. **Unique collections** (tags, categories, IDs)
47+
48+
## Examples
49+
50+
### Example 1: Creating Sets
51+
```python
52+
# Basic set creation
53+
colors = {"red", "green", "blue"}
54+
55+
# From list (removes duplicates)
56+
numbers = set([1, 2, 2, 3, 3, 3])
57+
print(numbers) # {1, 2, 3}
58+
59+
# Empty set (careful!)
60+
wrong = {} # This is a dictionary!
61+
correct = set() # This is an empty set
62+
```
63+
64+
### Example 2: Automatic Duplicate Removal
65+
```python
66+
# Duplicates automatically removed
67+
fruits = {"apple", "banana", "apple", "cherry"}
68+
print(fruits) # {'apple', 'banana', 'cherry'}
69+
70+
# Remove duplicates from list
71+
numbers = [1, 2, 2, 3, 3, 3, 4]
72+
unique = set(numbers)
73+
print(unique) # {1, 2, 3, 4}
74+
```
75+
76+
### Example 3: Adding and Removing
77+
```python
78+
fruits = {"apple", "banana"}
79+
80+
# Add single item
81+
fruits.add("cherry") # {'apple', 'banana', 'cherry'}
82+
83+
# Try to add duplicate (no effect)
84+
fruits.add("apple") # Still {'apple', 'banana', 'cherry'}
85+
86+
# Remove item
87+
fruits.remove("banana") # {'apple', 'cherry'}
88+
89+
# Safe remove (doesn't error if missing)
90+
fruits.discard("orange") # No error even though orange not in set
91+
```
92+
93+
### Example 4: Set Operations
94+
```python
95+
set_a = {1, 2, 3, 4}
96+
set_b = {3, 4, 5, 6}
97+
98+
# Union (all items from both)
99+
print(set_a | set_b) # {1, 2, 3, 4, 5, 6}
100+
101+
# Intersection (items in both)
102+
print(set_a & set_b) # {3, 4}
103+
104+
# Difference (in A but not B)
105+
print(set_a - set_b) # {1, 2}
106+
107+
# Symmetric difference (in either, not both)
108+
print(set_a ^ set_b) # {1, 2, 5, 6}
109+
```
110+
111+
### Example 5: Membership Testing
112+
```python
113+
# Fast membership check
114+
valid_users = {"alice", "bob", "charlie"}
115+
116+
user = "alice"
117+
if user in valid_users:
118+
print(f"Welcome, {user}!") # Fast lookup!
119+
```
120+
121+
### Example 6: Iterating Sets
122+
```python
123+
fruits = {"apple", "banana", "cherry"}
124+
125+
# Order may vary each run!
126+
for fruit in fruits:
127+
print(fruit)
128+
```
129+
130+
### Example 7: Set Comprehension
131+
```python
132+
# Create set of squares
133+
squares = {x**2 for x in range(10)}
134+
print(squares) # {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}
135+
136+
# Filter vowels from string
137+
text = "hello world"
138+
vowels = {char for char in text if char in "aeiou"}
139+
print(vowels) # {'o', 'e'}
140+
```
141+
142+
## Practice Exercises
143+
144+
### Beginner
145+
1. **Create Set**: Make a set of your 5 favorite foods
146+
2. **Remove Duplicates**: Convert list `[1,1,2,2,3,3]` to unique values
147+
3. **Add Items**: Start with empty set, add 5 numbers
148+
4. **Membership Test**: Check if "python" is in set of languages
149+
5. **Count Unique**: Count unique letters in "mississippi"
150+
151+
### Intermediate
152+
6. **Common Items**: Find common elements in two sets of numbers
153+
7. **Unique Words**: Find all unique words in a sentence
154+
8. **Set Difference**: Find items in set A but not in set B
155+
9. **Symmetric Diff**: Find items in either set but not both
156+
10. **Update Set**: Use update() to add multiple items at once
157+
158+
### Advanced
159+
11. **Tag System**: Build a tagging system with set operations
160+
12. **Duplicate Finder**: Find duplicate items across multiple lists
161+
13. **Set Math**: Implement set operations manually without operators
162+
14. **Frozen Sets**: Create immutable sets and understand use cases
163+
15. **Optimize Search**: Replace list membership with set for performance
164+
165+
## Common Mistakes to Avoid
166+
167+
### ❌ Mistake 1: Empty Set Syntax
168+
```python
169+
# WRONG: Creates dictionary, not set!
170+
empty = {}
171+
print(type(empty)) # <class 'dict'>
172+
173+
# CORRECT: Use set() function
174+
empty = set()
175+
print(type(empty)) # <class 'set'>
176+
```
177+
178+
### ❌ Mistake 2: Expecting Order
179+
```python
180+
# WRONG: Assuming sets maintain order
181+
numbers = {5, 2, 8, 1, 9}
182+
print(numbers) # Order not guaranteed!
183+
184+
# CORRECT: Use list if order matters
185+
numbers = [5, 2, 8, 1, 9]
186+
```
187+
188+
### ❌ Mistake 3: Trying to Index
189+
```python
190+
fruits = {"apple", "banana", "cherry"}
191+
# WRONG: Sets don't support indexing
192+
# print(fruits[0]) # TypeError!
193+
194+
# CORRECT: Convert to list if needed
195+
fruits_list = list(fruits)
196+
print(fruits_list[0])
197+
```
198+
199+
### ❌ Mistake 4: Mutable Items in Set
200+
```python
201+
# WRONG: Lists are mutable, can't be in sets
202+
# my_set = {[1, 2], [3, 4]} # TypeError!
203+
204+
# CORRECT: Use tuples (immutable)
205+
my_set = {(1, 2), (3, 4)} # Works!
206+
```
207+
208+
## Real-World Applications
209+
210+
### 1. Remove Duplicate Emails
211+
```python
212+
# Email list with duplicates
213+
emails = ["user@example.com", "admin@site.com", "user@example.com"]
214+
215+
# Get unique emails
216+
unique_emails = set(emails)
217+
print(f"Unique subscribers: {len(unique_emails)}")
218+
```
219+
220+
### 2. Tag System
221+
```python
222+
post1_tags = {"python", "programming", "tutorial"}
223+
post2_tags = {"python", "web", "flask"}
224+
225+
# Find common tags
226+
common = post1_tags & post2_tags
227+
print(f"Common tags: {common}") # {'python'}
228+
```
229+
230+
### 3. Permissions System
231+
```python
232+
user_permissions = {"read", "write"}
233+
required_permissions = {"read", "write", "delete"}
234+
235+
# Check if user has all required permissions
236+
if required_permissions.issubset(user_permissions):
237+
print("Access granted")
238+
else:
239+
missing = required_permissions - user_permissions
240+
print(f"Missing permissions: {missing}")
241+
```
242+
243+
### 4. Data Validation
244+
```python
245+
valid_statuses = {"pending", "approved", "rejected"}
246+
247+
def validate_status(status):
248+
return status.lower() in valid_statuses
249+
250+
print(validate_status("APPROVED")) # True (fast lookup!)
251+
```
252+
253+
## Challenge Projects
254+
255+
### Project 1: Student Enrollment System
256+
Create a system that:
257+
- Tracks students enrolled in courses using sets
258+
- Finds students in multiple courses
259+
- Identifies courses unique to each semester
260+
- Calculates total unique students
261+
262+
### Project 2: Social Network Friend Finder
263+
Build an app that:
264+
- Stores friends as sets for each user
265+
- Finds mutual friends (intersection)
266+
- Suggests friends (friends of friends)
267+
- Identifies exclusive friends
268+
269+
### Project 3: Inventory Deduplicator
270+
Develop a program that:
271+
- Removes duplicate SKUs from inventory
272+
- Finds items in warehouse A but not B
273+
- Merges inventory from multiple sources
274+
- Reports unique vs duplicate items
275+
276+
### Project 4: Word Analyzer
277+
Create a tool that:
278+
- Finds unique words in documents
279+
- Compares vocabulary between texts
280+
- Identifies unique words per author
281+
- Builds word frequency analysis
282+
283+
### Project 5: Access Control System
284+
Build a permissions manager:
285+
- Define role-based permissions as sets
286+
- Check user access levels
287+
- Combine permissions from multiple roles
288+
- Audit permission differences
289+
290+
## Navigation
291+
- [← Previous: Chapter 21 - Lists](../21-lists/)
292+
- [→ Next: Chapter 22 - Shopping Cart Program](../22-shopping-cart/)
293+
- [↑ Back to Main](../../README.md)

0 commit comments

Comments
 (0)