1.2.25 · D3Introduction to Programming (Python)

Worked examples — Sets — unique elements, set operations (union, intersection, difference)

4,085 words19 min readBack to topic

We use only the vocabulary the parent built: a set is an unordered, unique, hashable collection; the four operations are union | (OR), intersection & (AND), difference - (A not B), and symmetric difference ^ (exactly one). We lean on Boolean Logic — AND, OR, XOR and Hashing and Hashable Types underneath.


The scenario matrix

Before solving anything, let us enumerate every distinct situation a set problem can be. Each row is a "cell" — a case class that behaves differently. Every worked example below is tagged with the cell it covers, and together they fill the whole table.

Cell Scenario class What makes it its own case Covered by
C1 Duplicates & unhashable inputs uniqueness collapse; list/dict rejected Ex 1
C2 Empty set as an operand is the "identity/zero" of each op Ex 2
C3 Disjoint sets (share nothing) intersection empties out Ex 3
C4 Subset / superset (one inside other) one difference empties, other doesn't Ex 4
C5 Equal sets every op degenerates predictably Ex 5
C6 Asymmetry of - A-B ≠ B-A; sign of "who keeps what" Ex 6
C7 ^ reconstruction from and the identity, checked numerically Ex 7
C8 Mutation vs. return update changes A; union doesn't Ex 8
C9 Real-world word problem translate English → set algebra Ex 9
C10 Exam twist / chained ops precedence, <=, order-of-evaluation Ex 10
Figure — Sets — unique elements, set operations (union, intersection, difference)

What you are looking at (Figure s01): four small Venn diagrams in a 2×2 grid, each two overlapping black circles labelled A (left) and B (right). The region relevant to each operation is shaded red. Top-left "Union A | B" shades all three regions (both crescents plus the middle lens). Top-right "Intersection A & B" shades only the lens where the circles overlap. Bottom-left "Difference A - B" shades only the left crescent — A's part outside B. Bottom-right "Sym.diff A ^ B" shades both crescents but leaves the lens white. This is our map: every example below is literally "which of these red regions survive?"


Cell C1 — duplicates and unhashable inputs


Cell C2 — the empty set as an operand

The empty set (written set()) is the neutral element. Just like and in arithmetic, the empty set has a fixed effect on every operation.

Figure — Sets — unique elements, set operations (union, intersection, difference)

What you are looking at (Figure s02): on the left, a solid red-filled circle labelled A = {1,2,3}; on the right, a dashed empty box labelled E = set(). A red annotation across the top reads "A | E = A (identity)" pointing at the red circle — union adds nothing, so A survives whole. A black arrow points at the dashed empty box with the label "A & E = set() (zero)" — the shared region with an empty set is always empty. Use it to see why some answers keep A intact and others collapse to nothing.


Cell C3 — disjoint sets (share nothing)


Cell C4 — subset / superset (one lives inside the other)

Figure — Sets — unique elements, set operations (union, intersection, difference)

What you are looking at (Figure s03): one large black outlined circle labelled A = {1,2,3,4,5}, and inside it a small red-filled circle labelled B = {2,3} — B sits entirely within A's boundary, touching no edge. A red arrow points at the small circle with the label "B - A = set() (B inside A)". That single picture explains every answer below: the overlap is all of B, and B has no part sticking outside A, so B - A vanishes. Point at the red circle whenever you need to see why.


Cell C5 — equal sets


Cell C6 — the asymmetry of difference

The parent warned . Here is the case that makes the sign of "who keeps what" concrete.

Figure — Sets — unique elements, set operations (union, intersection, difference)

What you are looking at (Figure s04): two side-by-side Venn diagrams. In the left panel, titled "A - B = {1,2} (left crescent)", only A's left crescent is shaded red — B and the shared lens stay white. In the right panel, titled "B - A = {5,6} (right crescent)", only B's right crescent is shaded red. The two red regions never coincide — that mismatch is the visual meaning of "difference is directional." Glance at which crescent is red to know which set kept its members.


Cell C7 — rebuilding ^ from and


Cell C8 — mutation vs. return (the silent-bug case)


Cell C9 — a real-world word problem


Cell C10 — the exam twist (precedence & chaining)


Recall Quick self-test across the matrix

Disjoint sets: what is A & B? ::: set() — nothing shared. If B <= A, what is A | B? ::: A itself — B adds nothing new. Empty set is the identity of which op and the zero of which? ::: Identity of union, zero of intersection. Why is A - B ≠ B - A? ::: Difference is directional — it keeps the survivors of whoever is on the left. Does A.union(B) change A? ::: No — it returns a new set; A.update(B) is the mutating version. len(P | D) in terms of the parts? ::: len(P) + len(D) - len(P & D) (inclusion–exclusion). What does the symbol mean? ::: Symmetric difference — the same as A ^ B, "in exactly one."


Connections

  • 1.2.25 Sets — unique elements, set operations (union, intersection, difference) (Hinglish) — parent topic
  • Boolean Logic — AND, OR, XOR — the AND/OR/XOR behind &, |, ^
  • Hashing and Hashable Types — why lists get rejected in C1
  • Tuples — immutable sequences — the hashable fix
  • Lists — ordered, indexed collections — order matters there, not in C5
  • Big-O Notation & Time Complexity — why membership is fast