Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate30 min

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

Union intersection difference symmetric difference roadmap

1. The Venn diagram you must remember

Two-set Venn diagram with overlap C D and exclusive regions

We'll use these two sets throughout the lesson:

python
cricket = {"A", "B", "C", "D"}
tennis  = {"C", "D", "E", "F"}

2. Union (|) — everyone together

Union means combine everything without duplicates.

Union combines A B C D E F
python
cricket = {"A", "B", "C", "D"}
tennis  = {"C", "D", "E", "F"}

print(cricket | tennis)
print(cricket.union(tennis))

Output:

text
{'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.

Intersection highlights overlap C and D
python
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 minus tennis leaves A B only

Cricket only

python
print(cricket - tennis)  # {'A', 'B'}

Tennis only

python
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.

Symmetric difference keeps A B and E F, removes C D overlap
python
print(cricket ^ tennis)  # {'A', 'B', 'E', 'F'}

Think of students choosing clubs:

  • Both clubs → removed
  • Only one club → kept

6. All four operations together

Summary grid of union intersection difference symmetric difference
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.

B nested inside A — subset and superset hierarchy
python
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.

Two separate circles with no overlap — disjoint
python
a = {1, 2}
b = {7, 8}

print(a.isdisjoint(b))  # True

If there is even one common value:

python
a = {1, 2, 3}
b = {3, 8}

print(a.isdisjoint(b))  # False

9. 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.

python
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
Sports analytics summary visual

One-page revision sheet

Remember these four symbols:

UNION symbol pipe ALL

a | b

INTERSECTION symbol ampersand BOTH

a & b

DIFFERENCE symbol minus ONLY ME

a - b

SYMMETRIC symbol caret ONE ONLY

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 ^:

python
a - b   # only left-side exclusive elements
a ^ b   # both exclusive sides — removes overlap

Mistake 2 — thinking union() changes the original set:

python
a.union(b)   # returns NEW set
a.update(b)  # mutates a in place
a |= b       # same as update

Practice questions

Test yourself before File 11 — Frozenset Methods:

  1. Which operator returns all unique elements?
  2. What is the difference between A - B and B - A?
  3. Which operation removes the common values and keeps only exclusive ones?
  4. What does issubset() return?
  5. 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…