Set Operations & Comparison
Set operations let you combine, compare, and filter collections of data. Python supports all standard mathematical set operations: union, intersection, difference, and symmetric difference.
Learning Objectives
- 11.2.1.3 Use operations on sets
- 11.2.1.4 Compare sets
Conceptual Anchor
The Venn Diagram
Every set operation corresponds to a region in a Venn diagram. Union = everything in both circles. Intersection = the overlapping middle. Difference = one circle minus the overlap. Symmetric difference = both circles minus the overlap.
Rules & Theory
Set Operations
| Operation | Method | Operator | Result |
|---|---|---|---|
| Union | A.union(B) |
A | B |
All elements from both sets |
| Intersection | A.intersection(B) |
A & B |
Elements common to both |
| Difference | A.difference(B) |
A - B |
Elements in A but not in B |
| Symmetric Difference | A.symmetric_difference(B) |
A ^ B |
Elements in A or B but not both |
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print(A | B) # {1, 2, 3, 4, 5, 6, 7, 8} — Union
print(A & B) # {4, 5} — Intersection
print(A - B) # {1, 2, 3} — Difference (A not B)
print(B - A) # {6, 7, 8} — Difference (B not A)
print(A ^ B) # {1, 2, 3, 6, 7, 8} — Symmetric differenceSet Comparison
| Operation | Method | Operator | True when… |
|---|---|---|---|
| Subset | A.issubset(B) |
A <= B |
All elements of A are in B |
| Proper subset | — | A < B |
A ⊂ B and A ≠ B |
| Superset | A.issuperset(B) |
A >= B |
All elements of B are in A |
| Disjoint | A.isdisjoint(B) |
— | No common elements |
| Equality | — | A == B |
Same elements (order irrelevant) |
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {6, 7}
print(A <= B) # True — A is a subset of B
print(A < B) # True — A is a proper subset (A ≠ B)
print(B >= A) # True — B is a superset of A
print(A.isdisjoint(C)) # True — no common elements
print({3, 2, 1} == A) # True — same elements, order doesn't matterUpdate Operations
Add _update to modify the set in-place: A.update(B) is like
A = A | B. Similarly: intersection_update(),
difference_update(), symmetric_difference_update().
Worked Examples
1 Finding Common Students
math_class = {"Ali", "Dana", "Marat", "Aisha", "Bolat"}
cs_class = {"Aisha", "Bolat", "Kamila", "Dias", "Ali"}
# Who takes BOTH classes?
both = math_class & cs_class
print("Both:", both) # {'Ali', 'Aisha', 'Bolat'}
# Who takes EITHER class?
any_class = math_class | cs_class
print("Any:", any_class) # All 7 unique names
# Who takes Math but NOT CS?
math_only = math_class - cs_class
print("Math only:", math_only) # {'Dana', 'Marat'}2 Checking Subsets
required = {"Math", "Science", "English"}
student_courses = {"Math", "Science", "English", "Art", "PE"}
if required <= student_courses:
print("All required courses are taken ✓")
else:
missing = required - student_courses
print("Missing courses:", missing)3 Unique Characters in Two Strings
s1 = set(input("Word 1: ").lower()) # e.g., "hello"
s2 = set(input("Word 2: ").lower()) # e.g., "world"
print("Common letters:", s1 & s2) # {'l', 'o'}
print("Only in word 1:", s1 - s2) # {'h', 'e'}
print("In either (not both):", s1 ^ s2) # {'h', 'e', 'w', 'r', 'd'}Pitfalls & Common Errors
Difference is NOT Symmetric
A - B ≠ B - A. A - B gives elements in A but not B.
B - A gives elements in B but not A. These are usually different!
Confusing | and &
| (union) gives all elements. & (intersection)
gives only common elements. Think: | = OR (inclusive),
& = AND (restrictive).
Pro-Tips for Exams
Set Operation Strategy
- Draw a Venn diagram for complex set problems
A | Balways haslen(A | B) ≤ len(A) + len(B)A & Balways haslen(A & B) ≤ min(len(A), len(B))A ^ B=(A | B) - (A & B)— everything except the overlap- Subset check
A <= Bis the fastest way to test "all of A is in B"
Graded Tasks
Name the 4 set operations and their Python operators.
Given A = {1,2,3,4} and B = {3,4,5,6}, calculate by hand: A|B, A&B, A-B, A^B.
Write a program that reads two lists of friends and outputs: (a) mutual friends, (b) friends unique to each person.
Is it true that (A - B) | (B - A) == A ^ B for any sets A, B? Prove with an
example.
Build a "skill matcher" program: input required skills for a job and a candidate's skills. Output matched, missing, and extra skills using set operations.
Self-Check Quiz
{1,2,3} | {3,4,5} give?{1,2,3} & {3,4,5} give?{1,2} <= {1,2,3} True or False?{1,2,3} - {2,3,4} give?isdisjoint() check?