Lesson 4 of 15
Set Theory
Sets: The Foundation of Mathematics
A set is an unordered collection of distinct objects. Sets are described by membership: means belongs to .
Key Operations
| Operation | Notation | Definition |
|---|---|---|
| Union | ||
| Intersection | ||
| Difference | ||
| Complement | ||
| Power set | Set of all subsets of | |
| Cartesian product |
Power Set
If , then . Every element either is or isn't in a subset — binary choices.
def power_set(s):
result = [frozenset()]
for elem in sorted(s):
result = result + [subset | {elem} for subset in result]
return result
print(len(power_set({1, 2, 3}))) # 8 = 2^3
Cartesian Product
— the product rule for counting.
def cartesian_product(A, B):
return [(a, b) for a in sorted(A) for b in sorted(B)]
print(len(cartesian_product({1, 2}, {3, 4, 5}))) # 6 = 2 × 3
Your Task
Implement power_set(s) and cartesian_product(A, B) as shown above.
Pyodide loading...
Loading...
Click "Run" to execute your code.