Find a course

Module 3: Sets, Functions and RelationsLesson 6 of 1214 min

Sets and Set Operations

Describe sets in roster and set-builder notation, combine them with union, intersection, difference and complement, and count power sets and products.

Step 1 of 13Learn

Collections without order or repeats

A music app tracks which songs each user has liked. Alice's liked songs form a set: a collection of distinct objects where order doesn't matter and nothing appears twice. Liking a song twice doesn't add it twice, and the app doesn't care in what order she liked them.

Notation.

  • Roster form lists the elements: A = {2, 3, 5, 7}.
  • Set-builder form describes them: A = {x : x is a prime less than 10}, read "the set of x such that x is a prime less than 10."
  • x ∈ A means x is an element of A; 4 ∉ A means 4 is not.
  • |A| is the cardinality, the number of elements. Here |A| = 4.
  • ∅ is the empty set, with no elements: |∅| = 0.

Because order and repetition don't matter, {1, 2, 3}, {3, 1, 2} and {1, 1, 2, 3} are all the same set.

Common number sets have standard names: ℕ (natural numbers), ℤ (integers), ℚ (rationals) and ℝ (real numbers).

Subsets. A ⊆ B means every element of A is also in B. {2, 3} ⊆ {1, 2, 3}. Every set is a subset of itself, and ∅ is a subset of every set (vacuously: it has no element that fails to be in B). A ⊂ B, a proper subset, means A ⊆ B and A ≠ B.

Two sets are equal when each is a subset of the other. That's how you prove set equality: show every element of A is in B, then every element of B is in A.

Step 2 of 13Practice · True or false

The sets {1, 2, 2, 3} and {3, 2, 1} are equal.

Step 3 of 13Learn

Union, intersection, difference, complement

Set operations correspond directly to logical connectives:

  • Union A ∪ B = {x : x ∈ A or x ∈ B}: everything in either set (logical ∨).
  • Intersection A ∩ B = {x : x ∈ A and x ∈ B}: what they share (logical ∧).
  • Difference A − B (also written A \ B) = {x : x ∈ A and x ∉ B}: what's in A but not B.
  • Complement Aᶜ = {x ∈ U : x ∉ A}: everything in the universal set U that's not in A (logical ¬).

Example. U = {1, 2, …, 10}, A = {1, 2, 3, 4, 5}, B = {4, 5, 6, 7}.

  • A ∪ B = {1, 2, 3, 4, 5, 6, 7}
  • A ∩ B = {4, 5}
  • A − B = {1, 2, 3}
  • Bᶜ = {1, 2, 3, 8, 9, 10}

Sets with no common elements (A ∩ B = ∅) are disjoint.

Since set operations mirror logic, the logic laws carry over. De Morgan's laws for sets: (A ∪ B)ᶜ = Aᶜ ∩ Bᶜ and (A ∩ B)ᶜ = Aᶜ ∪ Bᶜ. "Not in A or B" means "not in A and not in B."

Venn diagrams draw sets as overlapping circles inside a rectangle for U, and they're great for intuition and for checking identities. But a diagram is not a proof; to prove an identity, use element-chasing or logical equivalences.

Databases use these operations all the time: SQL's UNION, INTERSECT and EXCEPT are exactly ∪, ∩ and −.

Step 4 of 13Practice · Fill in the blanks

Fill in the blanks

Let A = {1, 3, 5, 7, 9} and B = {3, 6, 9}. Then |A ∪ B| = , |A ∩ B| = , and |A − B| = .

Step 5 of 13Practice · Diagram

A = {1, 3, 5, 7, 9}, B = {3, 6, 9}

Tap the region that is A − B.

Tap the shape that answers the question, then press Check.

A = {1, 3, 5, 7, 9}, B = {3, 6, 9}Venn diagram: A, B, A only, B only, A + B (overlap).AB• 1• 5• 7• 6• 3• 9
A
B
A only

A − B: in A but not in B.

  • 1
  • 5
  • 7
B only

B − A.

  • 6
A + B (overlap)

A ∩ B: in both.

  • 3
  • 9

Step 6 of 13Learn

Counting: inclusion–exclusion, power sets and products

Inclusion–exclusion. To count a union, add the sizes and subtract the overlap, which was counted twice:

|A ∪ B| = |A| + |B| − |A ∩ B|.

Example. In a class of 40 students, 25 take Spanish, 18 take French and 7 take both. Then 25 + 18 − 7 = 36 take at least one language, and 40 − 36 = 4 take neither.

Power sets. The power set P(A) is the set of all subsets of A. For A = {a, b, c}:

P(A) = {∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}.

That's 8 = 2³ subsets. In general, |P(A)| = 2ⁿ when |A| = n, because building a subset means making n independent yes-or-no choices, one per element. This is why a pizza place offering 10 toppings can honestly advertise 1,024 possible pizzas, counting the plain one.

Cartesian products. A × B is the set of ordered pairs (a, b) with a ∈ A and b ∈ B. If A = {S, M, L} (sizes) and B = {red, blue} (colors), A × B has 3·2 = 6 pairs, one per product variant. In general |A × B| = |A|·|B|. Order matters in a pair: (S, red) is in A × B, while (red, S) is in B × A. The plane ℝ × ℝ = ℝ² is a Cartesian product, and a database table is a subset of a Cartesian product of its column domains.

Step 7 of 13Practice · Work it out

A set has 6 elements. How many subsets does it have, including the empty set and the set itself?

Step 8 of 13Chart

Each new element doubles the subsets

Each new element can be in or out of every existing subset, so |P(A)| = 2^|A|.

Each new element doubles the subsetsBar chart with 7 categories: 0: 1; 1: 2; 2: 4; 3: 8; 4: 16; 5: 32; 6: 64.0204060800123456Elements in the set1248163264
Each new element doubles the subsets
Elements in the setSubsets
01
12
24
38
416
532
664

Step 9 of 13Practice · Work it out

Of 100 job applicants, 60 know Python, 45 know SQL and 25 know both. How many know neither?

Step 10 of 13Practice · Diagram

100 applicants: Python and SQL

Tap the region that inclusion–exclusion subtracts because it was counted twice.

Tap the shape that answers the question, then press Check.

100 applicants: Python and SQLVenn diagram: Python (60), SQL (45), Python (60) only, SQL (45) only, Python (60) + SQL (45) (overlap).Python (60)SQL (45)• 35 Python only• 20 SQL only• 25 both
35 + 20 + 25 = 80 know at least one, so 20 know neither.
Python (60)
SQL (45)
Python (60) only
  • 35 Python only
SQL (45) only
  • 20 SQL only
Python (60) + SQL (45) (overlap)
  • 25 both

Step 11 of 13Note

Step 12 of 13Practice · Sort into groups

Is each statement true or false for A = {1, {2}, 3}?

Tap an item, then tap its group (or drag it there).

Step 13 of 13Practice · Choose one

If A has 3 elements and B has 4 elements, how many elements does A × B have?