Sets, Relations & Functions
Sets
Sets and their notation, subsets and power sets, union, intersection, difference and complement with Venn diagrams, set identities, Cartesian products and cardinality.
A set is an unordered collection of distinct objects called elements. Order and repetition do not matter: {1, 2, 3} = {3, 1, 2} = {1, 1, 2, 3}.
Notation
| Notation | Meaning |
|---|---|
| A = {2, 4, 6} | roster form: list the elements |
| A = {x ∈ ℤ : x is even, 0 < x < 8} | set-builder form: a rule |
| x ∈ A, x ∉ A | x is / is not an element of A |
| ∅ or {} | the empty set |
| U | the universal set — everything under discussion |
| |A| | cardinality: the number of elements |
| ℕ, ℤ, ℚ, ℝ | natural numbers, integers, rationals, reals |
Subsets and the power set
- A ⊆ B (subset): every element of A is in B. Every set is a subset of itself, and ∅ ⊆ A for every A.
- A ⊂ B (proper subset): A ⊆ B and A ≠ B.
- A = B exactly when A ⊆ B and B ⊆ A — the standard way to prove two sets are equal.
The power set P(A) is the set of all subsets of A. If |A| = n, then |P(A)| = 2ⁿ (each element is either in a subset or not).
from itertools import combinations
A = {"a", "b", "c"}
power_set = [set(c) for r in range(len(A) + 1) for c in combinations(sorted(A), r)]
print(len(power_set), "subsets:")
print([sorted(s) for s in power_set])8 subsets: [[], ['a'], ['b'], ['c'], ['a', 'b'], ['a', 'c'], ['b', 'c'], ['a', 'b', 'c']]
Set operations
| Operation | Symbol | Definition | Python |
|---|---|---|---|
| Union | A ∪ B | x ∈ A or x ∈ B | A | B |
| Intersection | A ∩ B | x ∈ A and x ∈ B | A & B |
| Difference | A − B | x ∈ A and x ∉ B | A - B |
| Symmetric difference | A ⊕ B | in exactly one of them | A ^ B |
| Complement | A′ or Ā | x ∈ U and x ∉ A | U - A |
Try your own sets:
Set identities
They mirror the laws of logic — ∪ is like ∨, ∩ like ∧, complement like ¬:
| Law | Identity |
|---|---|
| Identity | A ∪ ∅ = A, A ∩ U = A |
| Domination | A ∪ U = U, A ∩ ∅ = ∅ |
| Idempotent | A ∪ A = A, A ∩ A = A |
| Complement | A ∪ A′ = U, A ∩ A′ = ∅, (A′)′ = A |
| Commutative | A ∪ B = B ∪ A, A ∩ B = B ∩ A |
| Distributive | A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) |
| De Morgan | (A ∪ B)′ = A′ ∩ B′, (A ∩ B)′ = A′ ∪ B′ |
| Absorption | A ∪ (A ∩ B) = A, A ∩ (A ∪ B) = A |
| Difference | A − B = A ∩ B′ |
U = set(range(1, 11))
A, B = {1, 2, 3, 4, 5}, {4, 5, 6, 7}
comp = lambda X: U - X
print("(A ∪ B)′ =", sorted(comp(A | B)))
print("A′ ∩ B′ =", sorted(comp(A) & comp(B)))
print("De Morgan holds:", comp(A | B) == comp(A) & comp(B), comp(A & B) == comp(A) | comp(B))
print("|A ∪ B| = |A| + |B| - |A ∩ B|:", len(A | B), "=", len(A), "+", len(B), "-", len(A & B))(A ∪ B)′ = [8, 9, 10] A′ ∩ B′ = [8, 9, 10] De Morgan holds: True True |A ∪ B| = |A| + |B| - |A ∩ B|: 7 = 5 + 4 - 2
Cartesian product
The Cartesian product A × B is the set of all ordered pairs (a, b) with a ∈ A and b ∈ B. |A × B| = |A| · |B|.
from itertools import product
sizes = ["S", "M", "L"]
colours = ["red", "blue"]
pairs = list(product(sizes, colours))
print(len(pairs), pairs)6 [('S', 'red'), ('S', 'blue'), ('M', 'red'), ('M', 'blue'), ('L', 'red'), ('L', 'blue')]Order matters in pairs: (1, 2) ≠ (2, 1), so A × B ≠ B × A in general.
Cardinality of unions
For two sets: |A ∪ B| = |A| + |B| − |A ∩ B| — the intersection was counted twice. This is the inclusion–exclusion principle, extended in Counting.
Countable and uncountable
A set is countable if its elements can be listed as a sequence (a one-to-one match with ℕ). ℤ and ℚ are countable; Cantor's diagonal argument shows ℝ is uncountable — there are "more" real numbers than integers.
Test yourself
4 questions · pick an answer to check it instantly
- 1
How many subsets does a set with 5 elements have?
|P(A)| = 2ⁿ = 2⁵ = 32, including ∅ and A itself. - 2
If A = {1, 2, 3} and B = {3, 4}, what is A − B?
Elements of A that are not in B. - 3
By De Morgan's law, (A ∩ B)′ equals...
The complement of an intersection is the union of the complements. - 4
If |A| = 3 and |B| = 4, what is |A × B|?
Each of the 3 elements of A pairs with each of the 4 elements of B.