Set Theory
Set theory is the grammar of belonging: union gathers, intersection keeps only the shared, and complement keeps everything left out.
Why Does This Exist?
Data work is selection work: filter rows matching A, keep rows matching both A and B, exclude everything matching C. SQL WHERE, pandas masks, and probability events are all set operations wearing different clothes. Practitioners who think in sets write filters that are right the first time; those who do not double-count overlaps and leak exclusions.
The three operations below are the complete toolkit for combining selections, and the counting rule beside them prevents the most expensive mistake: adding two overlapping groups as if they were disjoint. Probability inherits all of it directly in probability foundations. The big picture is sets, logic and proofs.
The one idea you need first
A set is a collection of distinct objects, written , where order and repetition do not matter: and are the same set. Membership ("x is in A") is the only primitive; everything else is built from it.
Think of It Like This
Two reading clubs with shared members
Two book clubs meet in one building: club A has 7 members, club B has 5, and 2 keen readers joined both. Order pizza for "everyone in A or B" and 12 slices overshoots: the 2 shared members were counted twice, so 10 slices suffice. The shared pair is the intersection, everyone holding at least one card is the union, and the two neighbors who joined neither club are the complement.
Every filter bug is a pizza bug. Count the overlap twice and you over-order compute, under-size a test split, or double-weight training rows. The analogy stops at infinity: clubs are finite, but sets can be infinite, where counting gives way to subtler comparisons.
How It Actually Works
For sets and inside a universe of everything under discussion:
In plain words: union () gathers members of either; intersection () keeps only members of both; complement () keeps everything in the universe except 's members. The counting rule that ties them together:
where counts 's members. Subtract the overlap once because the plain sum counts it twice. The complement counts what is left: .
Worked example
Twelve students: 7 in club A, 5 in club B, 2 in both, universe :
- Intersection: .
- Union: in at least one club.
- A only: . B only: . Check: . Consistent.
- Complement of the union: in neither club.
Naive addition claims everyone joined something, hiding the 2 outsiders. The subtraction exposes them.
Code
Selections as Python sets, with the counting rule verified.
A = {f"a{i}" for i in range(7)}B = {f"a{i}" for i in range(5, 7)} | {f"b{i}" for i in range(3)}print(len(A & B)) # -> 2print(len(A | B)) # -> 10print(len(A | B) == len(A) + len(B) - len(A & B)) # -> TrueIntersection 2, union 10, and the counting identity holds exactly.
Watch Out For
Adding overlapping groups without subtracting
Dashboard totals that sum per-category counts double-count every shared member: users in two segments, errors matching two patterns, rows passing two filters. The total exceeds the population and nobody notices because each part looks right alone.
Always ask "can one item be in two buckets?" before summing. If yes, subtract the intersection or count the union directly from the raw membership.
Complementing against the wrong universe
"Not in A" depends entirely on what contains: excluding "spam" from "all mail" differs from excluding it from "inbox only." A complement taken against a silently shifted universe keeps or drops the wrong rows.
Name the universe in the same breath as the complement: "non-spam among all received mail." If changed upstream, recompute rather than reusing a stale excluded set.
The Quick Version
- Union gathers either-or members; intersection keeps both-and; complement keeps the rest.
- : subtract the double-counted overlap.
- 7 and 5 sharing 2 unite to 10, with 5 only-A, 3 only-B, 2 neither of 12.
- Order and repetition never matter inside a set.
- Name the universe before taking any complement.
- These three operations are the logic behind every data filter.