Lesson 10
Set Algebra — union(), intersection(), difference(), and more
Master Python set algebra with methods and operators: union, intersection, difference, symmetric difference, and subset relationships.
What you will learn
Union · Intersection · Difference · Symmetric Difference · Subsets
Set algebra is one of Python's most useful features for comparing groups of data. Think of it as mathematics with collections.
| Symbol | Meaning |
|---|---|
| ` | ` |
& |
Intersection — shared only |
- |
Difference — left exclusive |
^ |
Symmetric difference — one side only |
Memory formula:
|= ALL ·&= BOTH ·-= ONLY ME ·^= EITHER ONE
After File 09 — Set Methods, you can add and remove set members. This lesson compares whole groups with algebra.
Learning roadmap
1. The Venn diagram you must remember
We'll use these two sets throughout the lesson:
cricket = {"A", "B", "C", "D"}
tennis = {"C", "D", "E", "F"}2. Union (|) — everyone together
Union means combine everything without duplicates.
cricket = {"A", "B", "C", "D"}
tennis = {"C", "D", "E", "F"}
print(cricket | tennis)
print(cricket.union(tennis))Output:
{'A', 'B', 'C', 'D', 'E', 'F'}Both expressions produce the same result.
| Operator | Method |
|---|---|
| `a | b` |
3. Intersection (&) — common elements
Intersection means only the shared values.
print(cricket & tennis) # {'C', 'D'}
print(cricket.intersection(tennis))Think of interview attendance:
- Cricket players = invited
- Tennis players = registered
- Intersection = invited and registered
4. Difference (-) — only me
Difference removes everything that exists in the second set.
Cricket only
print(cricket - tennis) # {'A', 'B'}Tennis only
print(tennis - cricket) # {'E', 'F'}Order matters!
| Expression | Result |
|---|---|
A - B |
Only A's exclusive elements |
B - A |
Only B's exclusive elements |
5. Symmetric difference (^) — exactly one group
This is the most confusing operator for beginners.
Present in A OR B, but NOT BOTH.
print(cricket ^ tennis) # {'A', 'B', 'E', 'F'}Think of students choosing clubs:
- Both clubs → removed
- Only one club → kept
6. All four operations together
| Symbol | Name | Meaning |
|---|---|---|
| ` | ` | Union |
& |
Intersection | Shared |
- |
Difference | Only left |
^ |
Symmetric difference | Exclusive |
7. Subset and superset
These do not create new sets — they return True or False.
A = {10, 20, 30}
B = {10, 20}
print(B.issubset(A)) # True
print(A.issuperset(B)) # True
print(B <= A) # True — operator form
print(A >= B) # True| Relationship | Meaning |
|---|---|
| Subset | Small inside big |
| Superset | Big contains small |
8. Disjoint sets
Disjoint means no common values.
a = {1, 2}
b = {7, 8}
print(a.isdisjoint(b)) # TrueIf there is even one common value:
a = {1, 2, 3}
b = {3, 8}
print(a.isdisjoint(b)) # False9. Methods vs operators
Both styles are equally valid.
| Operator | Method |
|---|---|
| `a | b` |
a & b |
a.intersection(b) |
a - b |
a.difference(b) |
a ^ b |
a.symmetric_difference(b) |
In-place update variants
| Operator | In-place method |
|---|---|
| `a | = b` |
a &= b |
a.intersection_update(b) |
a -= b |
a.difference_update(b) |
a ^= b |
a.symmetric_difference_update(b) |
Which should you use?
- Short code: operators (
|,&,-,^) - Readable beginner code: methods (
union(),intersection())
Mini project — sports club analytics
Imagine a school database.
cricket = {"Ravi", "Anil", "Sunil", "Sachin"}
football = {"Sachin", "Rahul", "Vijay", "Sunil"}
print("All students:", cricket | football)
print("Dual players:", cricket & football)
print("Only cricket:", cricket - football)
print("Only football:", football - cricket)
print("Exactly one sport:", cricket ^ football)Expected results
| Question | Expression |
|---|---|
| Total unique players | `cricket |
| Play both sports | cricket & football |
| Cricket only | cricket - football |
| Football only | football - cricket |
| One sport only | cricket ^ football |
One-page revision sheet
Remember these four symbols:
a | b
a & b
a - b
a ^ b
| Task | Syntax |
|---|---|
| All unique | `a |
| Shared only | a & b |
| Left exclusive | a - b |
| One side only | a ^ b |
| Subset check | b.issubset(a) |
| No overlap | a.isdisjoint(b) |
Common interview mistakes
Mistake 1 — confusing - with ^:
a - b # only left-side exclusive elements
a ^ b # both exclusive sides — removes overlapMistake 2 — thinking union() changes the original set:
a.union(b) # returns NEW set
a.update(b) # mutates a in place
a |= b # same as updatePractice questions
Test yourself before File 11 — Frozenset Methods:
- Which operator returns all unique elements?
- What is the difference between
A - BandB - A? - Which operation removes the common values and keeps only exclusive ones?
- What does
issubset()return? - Which method checks whether two sets have no overlap?
Knowledge check
Q1: Operator for all unique elements?
Answer: | (union).
Q2: A - B vs B - A?
Answer: Direction matters — each returns only the left set's exclusive elements.
Q3: Remove common values, keep exclusive?
Answer: ^ (symmetric difference).
Q4: issubset() return type?
Answer: True or False.
Q5: Check no overlap?
Answer: isdisjoint().
Loading playground…