Set Operations
Sets combine with a handful of operations that mirror the logical connectives: union is OR, intersection is AND, complement is NOT. That correspondence means every law of logic gives a law of sets, and you can prove set identities with the same tools.
The operations
| Operation | Math | Definition | Python |
|---|---|---|---|
| union | A | B |
||
| intersection | A & B |
||
| difference | (or ) | A - B |
|
| symmetric difference | in exactly one of , | A ^ B |
|
| complement | for a domain | D - A |
Sets with no common element are disjoint: .
D = set(range(1, 11))
A = {x for x in D if x % 2 == 0} # even
B = {x for x in D if x % 3 == 0} # multiples of 3
print("A =", sorted(A), " B =", sorted(B))
print("A union B =", sorted(A | B))
print("A inter B =", sorted(A & B))
print("A - B =", sorted(A - B), " B - A =", sorted(B - A))
print("A xor B =", sorted(A ^ B))
print("complement of A =", sorted(D - A))A = [2, 4, 6, 8, 10] B = [3, 6, 9]
A union B = [2, 3, 4, 6, 8, 9, 10]
A inter B = [6]
A - B = [2, 4, 8, 10] B - A = [3, 9]
A xor B = [2, 3, 4, 8, 9, 10]
complement of A = [1, 3, 5, 7, 9]
Set identities
Every propositional equivalence becomes a set identity. For example De Morgan's laws:
and the distributive law .
Proof of the distributive law (chain of iffs). For any :
Since is in the left set exactly when it is in the right set, the sets are equal.
The same identity can be tested on random sets. A test like this does not replace the proof, but it catches wrong identities quickly:
import random
random.seed(7)
U = range(20)
rand_set = lambda: {x for x in U if random.random() < 0.5}
ok_dist = ok_morgan = True
for _ in range(1000):
A, B, C = rand_set(), rand_set(), rand_set()
ok_dist &= A & (B | C) == (A & B) | (A & C)
ok_morgan &= set(U) - (A | B) == (set(U) - A) & (set(U) - B)
print("distributive law held:", ok_dist, " De Morgan held:", ok_morgan)
# a FALSE "identity": A - (B - C) == (A - B) - C
A, B, C = {1, 2, 3}, {2, 3}, {3}
print("A - (B - C) =", A - (B - C), " (A - B) - C =", (A - B) - C)distributive law held: True De Morgan held: True
A - (B - C) = {1, 3} (A - B) - C = {1}
Difference is not associative: keeps the elements of that removed from the subtraction.
Venn diagrams and counting
For finite sets, the size of a union follows from the overlap:
The elements of are counted twice in , so we subtract them once. In the example above, . The general version for many sets is inclusion-exclusion.
In software
Set operations are built into SQL: UNION, INTERSECT and EXCEPT (difference). Python's sets make tasks like "which users are in the beta group but not admins" one expression, beta - admins. Bitsets store a set of small integers as the bits of an integer, so union is | and intersection is & on whole machine words at once.
Exercises
Exercise 1: Compute
With , , : find , , , and .
Answer
, , , , .
Solution
. The complement of the union is what is left of : .
Exercise 2: Prove an identity
Prove .
Answer
.
Solution
Each step uses only a definition: difference, then complement (relative to the domain), then intersection. Since membership is equivalent for every , the sets are equal. This identity is how SQL engines often implement EXCEPT: an intersection with "not in".
Exercise 3: Union size
In a class of 40 students, 25 take Python, 18 take SQL and 9 take both. How many take neither?
Answer
Solution
Exercise 4: Test a conjecture (code)
Is always true? Test on random sets.
Answer
Yes (no counterexample appears, and it follows from the distributive law).
Solution
by distributivity.
import random
random.seed(1)
r = lambda: {x for x in range(12) if random.random() < 0.5}
print(all((A | B) - C == (A - C) | (B - C) for A, B, C in ((r(), r(), r()) for _ in range(2000))))True
Test yourself
Which logical connective corresponds to set intersection?
- OR
- AND
- NOT
- XOR
By De Morgan, equals…
If , and , what is ?
- 3
- 5
- 17
- 2