Operations on Sets — Exercises
With Python solutions
By Pr. El Hadiq Zouhair
1Basic union, intersection, difference easy
Let $A = \{2, 4, 6, 8\}$ and $B = \{1, 2, 3, 4, 5\}$. Compute:
$A \cup B$, $A \cap B$, $A \setminus B$, $B \setminus A$.
Solution
Python
A = {2, 4, 6, 8}
B = {1, 2, 3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5, 6, 8}
print(A & B) # {2, 4}
print(A - B) # {6, 8}
print(B - A) # {1, 3, 5}
2N-ary operations easy
Given the family of sets L = [{1,2,3}, {2,3,4}, {3,4,5}, {3,5,7}], compute:
- $\displaystyle\bigcup_{i} L_i$
- $\displaystyle\bigcap_{i} L_i$
Solution
Python
L = [{1,2,3}, {2,3,4}, {3,4,5}, {3,5,7}]
print(set().union(*L)) # {1, 2, 3, 4, 5, 7}
print(set.intersection(*L)) # {3}
3Complement with a universe easy
With universe $U = \{1, 2, \dots, 10\}$ and $A = \{1, 3, 5, 7, 9\}$ (odd numbers), compute $A'$.
Then verify $A \cup A' = U$ and $A \cap A' = \varnothing$.
Solution
Python
U = set(range(1, 11))
A = {1, 3, 5, 7, 9}
A_c = U - A
print(A_c) # {2, 4, 6, 8, 10}
assert A | A_c == U
assert A & A_c == set()
print("Both identities hold.")
4Operator precedence medium
Complement > Intersection > Union. With $U = \{1..10\}$,
$A = \{1,2,3\}$, $B = \{2,3,4\}$, $C = \{3,4,5\}$, evaluate by hand, then in Python:
- $A \cup B \cap C$ (meaning $A \cup (B \cap C)$)
- $(A \cup B) \cap C$
- $A' \cap B \cup C$ (meaning $(A' \cap B) \cup C$)
Solution
Python
U = set(range(1, 11))
A, B, C = {1,2,3}, {2,3,4}, {3,4,5}
print(A | (B & C)) # {1, 2, 3, 4}
print((A | B) & C) # {3, 4}
print(((U - A) & B) | C) # {3, 4, 5}
5The 8 regions of a 3-set Venn medium
With $A = \{1..7\}$, $B = \{3..9\}$, $C = \{5..11\}$, write a function that returns the
contents of each of the 8 regions of the Venn diagram of $A, B, C$. Which regions are empty?
Solution
Python
from itertools import product
def venn_regions(A, B, C, U=None):
U = U or (A | B | C)
regions = {}
for bits in product([0, 1], repeat=3):
sel = [(A, B, C)[i] if b else (U - (A, B, C)[i]) for i, b in enumerate(bits)]
regions[bits] = sel[0] & sel[1] & sel[2]
return regions
A, B, C = set(range(1,8)), set(range(3,10)), set(range(5,12))
for k, v in venn_regions(A, B, C).items():
print(k, sorted(v))
Empty regions: $(0,0,0)$, $(0,1,0)$, $(1,0,1)$.
6Inclusion–Exclusion (2 sets) medium
Pick any $A, B \subseteq \{1, \dots, 20\}$ randomly and verify
$|A \cup B| = |A| + |B| - |A \cap B|$ holds on 1000 random pairs.
Solution
Python
import random
U = list(range(1, 21))
for _ in range(1000):
A = set(random.sample(U, random.randint(0, 20)))
B = set(random.sample(U, random.randint(0, 20)))
assert len(A | B) == len(A) + len(B) - len(A & B)
print("1000 random pairs verified ✓")
7De Morgan by brute force medium
For $U = \{1, 2, 3, 4\}$, check $(A \cup B)' = A' \cap B'$ for every pair of subsets of $U$
(there are $2^4 \times 2^4 = 256$ pairs).
Solution
Python
from itertools import combinations
U = {1, 2, 3, 4}
subsets = []
for r in range(len(U) + 1):
subsets += [set(c) for c in combinations(U, r)]
for A in subsets:
for B in subsets:
lhs = U - (A | B)
rhs = (U - A) & (U - B)
assert lhs == rhs
print("De Morgan holds for all 256 (A, B) pairs.")
8Distributivity check medium
Prove (by exhaustive check on $U = \{1, 2, 3\}$) that
$A \cap (B \cup C) = (A \cap B) \cup (A \cap C)$ for all $A, B, C \subseteq U$.
Solution
Python
from itertools import combinations
U = {1, 2, 3}
subsets = [set(c) for r in range(len(U)+1) for c in combinations(U, r)]
count = 0
for A in subsets:
for B in subsets:
for C in subsets:
assert A & (B | C) == (A & B) | (A & C)
count += 1
print(count, "triples verified ✓") # 8^3 = 512
9Membership table medium
Print the full membership (truth) table for $(A \cup B)'$ and $A' \cap B'$ and show they are equal.
Solution
Python
from itertools import product
header = f"{'A':>5} {'B':>5} {'(A∪B)\\'':>10} {'A\\'∩B\\'':>10}"
print(header)
print("-" * len(header))
for a, b in product([True, False], repeat=2):
lhs = not (a or b)
rhs = (not a) and (not b)
print(f"{a!s:>5} {b!s:>5} {lhs!s:>10} {rhs!s:>10}")
Output
A B (A∪B)' A'∩B'
True True False False
True False False False
False True False False
False False True True
10Simplify with SymPy hard
Using SymPy's simplify (boolean), show that
$A \cup (A \cap B) \equiv A$ (absorption).
Then show
$(A \cap B) \cup (A \cap B') \equiv A$ (a useful "splitting" identity).
Solution
Python — SymPy
from sympy import symbols, And, Or, Not, simplify, Equivalent
A, B = symbols('A B')
print(simplify(Or(A, And(A, B)))) # A
print(simplify(Equivalent(Or(And(A, B),
And(A, Not(B))), A)))# True
11Symmetric difference identities hard
Show, by brute force on $U = \{1, 2, 3, 4\}$, that:
- $A \,\triangle\, B = (A \cup B) \setminus (A \cap B)$
- $A \,\triangle\, A = \varnothing$
- $A \,\triangle\, B \,\triangle\, C$ is associative
Solution
Python
from itertools import combinations
U = {1, 2, 3, 4}
subs = [set(c) for r in range(len(U)+1) for c in combinations(U, r)]
for A in subs:
for B in subs:
assert A ^ B == (A | B) - (A & B)
assert A ^ A == set()
for B in subs:
for C in subs:
assert (A ^ B) ^ C == A ^ (B ^ C)
print("All symmetric-difference identities verified ✓")
12Word problem — 3-set Venn hard
A survey of 200 people about three streaming services (N, P, D) reports:
- 120 use N, 90 use P, 70 use D.
- 50 use N and P, 30 use N and D, 25 use P and D.
- 15 use all three.
How many use:
- at least one service?
- exactly one service?
- exactly two services?
- none?
Solution
Python
N_, P_, D_ = 120, 90, 70
NP, ND, PD = 50, 30, 25
NPD = 15
total = 200
# at least one (inclusion-exclusion)
at_least_one = N_ + P_ + D_ - NP - ND - PD + NPD
none = total - at_least_one
# exactly two (sum of pairwise minus 3×triple)
exactly_two = (NP - NPD) + (ND - NPD) + (PD - NPD)
# exactly one
only_N = N_ - NP - ND + NPD
only_P = P_ - NP - PD + NPD
only_D = D_ - ND - PD + NPD
exactly_one = only_N + only_P + only_D
print("at least one :", at_least_one)
print("exactly one :", exactly_one)
print("exactly two :", exactly_two)
print("none :", none)
Output
at least one : 190
exactly one : 125
exactly two : 50
none : 10
Going further
- Generalize
venn_regions from 3 to $n$ sets.
- Visualize exercise 12 with
matplotlib_venn.venn3.
- Re-prove distributivity using
sympy.simplify(Equivalent(...)) instead of brute force.
- Prove $A \,\triangle\, B = (A \cap B') \cup (A' \cap B)$ symbolically with SymPy.
Open the main lesson for the matching theory.