Sets and Set Operations
3546 words
18 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Sets and Set Operations ## 🎯 Learning Objectives By the end of this topic, you will be able to: 1. **Classify** numbers into $\mathbb{N}, \mathbb{Z}, \mathbb{Q}, \mathbb{R}, \mathbb{C}$ and understand their hierarchy 2.

Sets and Set Operations
🎯 Learning Objectives
By the end of this topic, you will be able to:
- Classify numbers into N,Z,Q,R,C and understand their hierarchy
- Define a set using roster form and set-builder notation
- Perform set operations — union, intersection, complement, difference, symmetric difference
- Compute cardinalities using the inclusion–exclusion principle
- Construct power sets and determine the number of subsets
- Interpret set relationships using Venn diagrams
📋 Prerequisites
- Basic arithmetic — addition, subtraction, multiplication, division, modulo (remainder)
- Logical connectives — AND (∧), OR (∨), NOT (¬) — first encountered in primary school logic
📖 Core Content
1.1 Number Systems — The Foundation
1.1.1 Intuition: What Are Numbers?
Numbers are humanity's way of keeping count. Before written language, shepherds notched tally sticks to track sheep — one notch per animal. That simple act of matching is the root of all number systems. Over centuries, we discovered we needed bigger and more flexible kinds of numbers to solve harder problems:
- Counting → Natural numbers
- Debt / temperature → Integers
- Sharing a pizza → Rational numbers
- The diagonal of a square → Irrational numbers (Real numbers)
- Square roots of negatives → Complex numbers
💡 Why this matters: In data science, the type of number determines what operations are valid. You cannot average ZIP codes (they are labels, not quantities). You cannot take the log of a negative number (unless you use complex numbers). Understanding number systems prevents subtle bugs in data pipelines.
1.1.2 Formal Definitions
| Set | Symbol | Description | Examples |
|---|---|---|---|
| Natural numbers | N | Counting numbers including 0 | 0,1,2,3,… |
| Integers | Z | Whole numbers, positive & negative | …,−2,−1,0,1,2,… |
| Rational numbers | Q | Fractions qp where p,q∈Z, q=0 | 21,−43,5=15 |
| Real numbers | R | All rational & irrational numbers | 2,π,e,0.333… |
| Complex numbers | C | Numbers of form a+bi , i=−1 | 1+2i,−3i |
Key hierarchical relationship:
Every natural number is an integer, every integer is rational, every rational is real, every real is complex — but the reverse is not true.
(Diagram)
1.1.3 Important Properties
Discreteness vs. Density:
- Integers are discrete. Between 2 and 3 there is no other integer. You can list them in order: …,−2,−1,0,1,2,…
- Rationals are dense. Between any two rational numbers, there is always another rational. For example, between 31 and 21, the average 21/3+1/2=125 lies between them.
- Reals are dense too. Between any two reals, there is another real. But reals also include irrationals like 2 and π, which cannot be expressed as qp. Prime Numbers: A prime p is a natural number >1 whose only factors are 1 and p itself. Examples: 2,3,5,7,11,13,… Every integer can be written uniquely as a product of primes (prime factorization). Example: 84=22×3×7.
🔍 Edge case: 1 is not prime — it has only one factor (itself). There is no largest prime (proved by Euclid).
1.1.4 Worked Examples
Example 1.1 (Easy): Classify each number: 4, −7, 722, 2, 4+−1
| Number | N ? | Z ? | Q ? | R ? | C ? |
|---|---|---|---|---|---|
| 4 | ✓ | ✓ | ✓ | ✓ | ✓ |
| −7 | ✗ | ✓ | ✓ | ✓ | ✓ |
| 722 | ✗ | ✗ | ✓ | ✓ | ✓ |
| 2 | ✗ | ✗ | ✗ | ✓ | ✓ |
| 4+−1 | ✗ | ✗ | ✗ | ✗ | ✓ |
Example 1.2 (Medium): Find the prime factorization of 1260.
Step 1: Start dividing by the smallest prime (2): 1260÷2=630
Step 2: 630÷2=315 (2 doesn't divide 315)
Step 3: 315÷3=105 (3 divides 315 since 3+1+5=9 divisible by 3)
Step 4: 105÷3=35
Step 5: 35÷5=7
Step 6: 7 is prime.
Example 1.3 (Exam-style): Prove that 2 is irrational.
ProofProof by contradiction. Assume 2=qp where p,q∈Z, q=0, and gcd(p,q)=1 (fraction is in lowest terms).Squaring both sides: 2=q2p2⟹p2=2q2This means p2 is even, so p must be even. Write p=2k.Then (2k)2=2q2⟹4k2=2q2⟹q2=2k2So q2 is even, hence q is even. But then p and q are both even — they share a factor of 2, contradicting gcd(p,q)=1.Thus 2 cannot be expressed as qp; it is irrational. ∎
1.2 The Concept of a Set
1.2.1 Intuition: What Is a Set?
A set is simply a collection of distinct objects considered as a single thing. Think of a shopping bag: it holds items (apples, milk, bread), and you carry the bag as one unit. The items inside are the elements (or members) of the set. A set cares only about which items are inside — not the order they're arranged, not whether they're repeated.
💡 Why this matters: Sets are the mathematical language for "grouping things." When we talk about "all users in the database" or "all products whose price > $100," we're implicitly working with sets. SQL'sUNION,INTERSECT, andEXCEPTare direct translations of set operations.
1.2.2 Formal Definition
A set is an unordered collection of distinct elements.
- Roster form: List elements inside curly braces. A={1,2,3,4}
- Set-builder form: Describe the property that elements satisfy. A={x∣x∈N, x≥1, x≤4} Read as: "the set of all x such that x is a natural number, x≥1, and x≤4"
| Symbol | Meaning | Example |
|---|---|---|
| ∈ | Element of | 2∈{1,2,3} |
| ∈/ | Not element of | 4∈/{1,2,3} |
| ⊆ | Subset | {1,2}⊆{1,2,3} |
| ⊂ | Proper subset | {1,2}⊂{1,2,3} |
| ∅ | Empty set | {} or ∅ |
| ∣ or : | "Such that" | {x∣x>0} |
Key properties:
- Unordered: {1,2,3}={3,1,2}
- No duplicates: {1,1,2}={1,2}
- Empty set ∅={} is a subset of every set.
- Cardinality ∣A∣ is the number of distinct elements in A.
1.2.3 Set Comprehension Pattern
Set-builder notation follows a three-step pattern:
- Generate: Draw elements from an existing set
- Filter: Keep only those satisfying a condition
- Transform: (Optional) Apply a function to each kept element Examples:
- Even integers: {x∣x∈Z, xmod2=0}
- Perfect squares: {n2∣n∈N}
- Rational numbers in lowest terms: {p/q∣p,q∈Z, q=0, gcd(p,q)=1}
1.2.4 Power Set
The power set of a set A, denoted P(A) or 2A, is the set of all subsets of A, including the empty set and A itself.
If ∣A∣=n, then ∣P(A)∣=2n.
Reason: Each of the n elements can either be in or out of a given subset — 2 choices per element → 2n total subsets.
Example: A={a,b}
∣A∣=2, ∣P(A)∣=22=4.
(Diagram)
1.2.5 Worked Examples
Example 2.1 (Easy): Write the set {x∣x∈Z, −3<x≤3} in roster form.
Solution: The integers greater than −3 and up to and including 3 are: −2,−1,0,1,2,3.
Example 2.2 (Medium): How many subsets does A={1,2,3,4,5} have?
Step 1: ∣A∣=5
Step 2: Number of subsets = 25=32
Step 3: Number of proper subsets (excluding A itself) = 32−1=31
Example 2.3 (Hard): Let A={1,2,{3,4}}. Find ∣A∣ and P(A).
Step 1: Count elements in A. The elements are: 1, 2, and {3,4} (a set is a single element). So ∣A∣=3.
Step 2: P(A) has 23=8 elements:
⚠️ Edge case: {3,4} is a single element of A, so its cardinality counts as 1, not 2. This is a common mistake.
1.3 Set Operations
1.3.1 Intuition: Combining Sets
Sets can be combined just like numbers. If sets A and B represent "users who liked product X" and "users who liked product Y", then:
- Union (A∪B): Users who liked X OR Y (or both)
- Intersection (A∩B): Users who liked both X AND Y
- Difference (A∖B): Users who liked X but NOT Y
- Complement (Ac or A): Users who did NOT like X (Diagram)
| Operation | Notation | Definition | Venn Diagram |
|---|---|---|---|
| Union | A∪B | {x∣x∈A or x∈B} | |
| Intersection | A∩B | {x∣x∈A and x∈B} | |
| Difference | A∖B | {x∣x∈A and x∈/B} | |
| Complement | Ac or A | {x∈U∣x∈/A} | |
| Symmetric Diff. | A△B | (A∖B)∪(B∖A) |
🔍 Key relationship: A△B=(A∪B)∖(A∩B)
1.3.2 Properties of Set Operations
| Property | Formula |
|---|---|
| Commutative | A∪B=B∪A , A∩B=B∩A |
| Associative | (A∪B)∪C=A∪(B∪C) |
| Distributive | A∩(B∪C)=(A∩B)∪(A∩C) |
| Identity | A∪∅=A , A∩U=A |
| Complement | A∪Ac=U , A∩Ac=∅ |
| Idempotent | A∪A=A , A∩A=A |
De Morgan's Laws (very important):
- (A∪B)c=Ac∩Bc
- (A∩B)c=Ac∪Bc
💡 Memory Aid: "Break the parenthesis, flip the operator." Union becomes intersection, intersection becomes union, complement each term.
1.3.3 Cardinality and Inclusion–Exclusion
For finite sets:
| Situation | Formula |
|---|---|
| Two sets, possibly overlapping | $ |
| Two disjoint sets | $ |
| Three sets | $ |
The pattern: add all singles, subtract all pairwise intersections, add all triple intersections.
1.3.4 Worked Examples
Example 3.1 (Easy): Let U={1,2,3,4,5,6,7,8,9,10}, A={1,2,3,4}, B={3,4,5,6}. Find A∪B, A∩B, A∖B, Ac.
Step 1: A∪B={1,2,3,4,5,6} (all elements in A or B)
Step 2: A∩B={3,4} (common to both)
Step 3: A∖B={1,2} (in A but not in B)
Step 4: Ac=U∖A={5,6,7,8,9,10}
Example 3.2 (Medium): In a class of 50 students, 30 like Mathematics, 20 like Physics, and 10 like both. How many like neither?
Step 1: ∣M∣=30, ∣P∣=20, ∣M∩P∣=10, ∣U∣=50
Step 2: ∣M∪P∣=∣M∣+∣P∣−∣M∩P∣=30+20−10=40
Step 3: Like neither =∣U∣−∣M∪P∣=50−40=10
Example 3.3 (Hard): In a survey of 100 people: 60 read Times of India, 50 read The Hindu, 40 read Indian Express, 20 read TOI and Hindu, 15 read TOI and Express, 10 read Hindu and Express, 5 read all three. How many read at least one?
Step 1: Apply inclusion–exclusion for three sets:
∣T∪H∪E∣=∣T∣+∣H∣+∣E∣−∣T∩H∣−∣T∩E∣−∣H∩E∣+∣T∩H∩E∣
=60+50+40−20−15−10+5
=150−45+5=110
Wait — this exceeds 100! What's wrong?
Step 2: Check: The data must mean that the 5 who read all three are already counted in the pairwise intersections. But ∣T∩H∣=20 includes those 5, etc. The formula is correct: =150−45+5=110.
So there's inconsistency in the data (a common real-world issue). If the survey is accurate, some counts must be adjusted.
⚠️ Edge case: Inclusion–exclusion can exceed the total population, revealing measurement error.
📐 Key Formulas — Summary Table
| Concept | Formula | When to Use |
|---|---|---|
| Cardinality (2 sets) | $\ | A \cup B\ |
| Cardinality (3 sets) | $\ | A \cup B \cup C\ |
| Number of subsets | 2n | Power set problems |
| De Morgan (union) | (A∪B)c=Ac∩Bc | Simplifying complements |
| De Morgan (intersection) | (A∩B)c=Ac∪Bc | Simplifying complements |
| Symmetric difference | A△B=(A∖B)∪(B∖A) | XOR-like operations |
| Number of elements | n(A)=n(A∪B)+n(A∩B)−n(B) | Missing value problems |
| Prime factorization | Unique product of primes | GCD, LCM, simplifying fractions |
| Density property | Between any two reals, another real exists | Rational/real number theory |
⚠️ Common Pitfalls
Pitfall 1: Confusing ∅ and {∅}
Mistake: Thinking ∅ and {∅} are the same.
Why: ∅ is the empty set (no elements). {∅} is a set containing one element — the empty set. Their cardinalities differ: ∣∅∣=0, ∣{∅}∣=1.
Correct approach:
- ∅⊆{∅} (true — empty set is subset of every set)
- ∅∈{∅} (true — the empty set is an element of {∅})
- {∅}⊆∅ (false — {∅} has an element but ∅ has none)
Pitfall 2: Double-Counting in Set Cardinality Problems
Mistake: Adding the sizes of two overlapping sets without subtracting the intersection.
Example: If 20 take Math, 30 take Physics, 5 take both, total = 20 + 30 = 50 (INCORRECT).
Correct: 20+30−5=45.
How to catch: Draw a Venn diagram. The intersection region is counted twice if you simply add.
Pitfall 3: Confusing ⊂ (proper subset) with ⊆ (subset)
Mistake: Writing A⊂B when A=B is possible.
Correct:
- A⊆B means every element of A is also in B (possibly equal sets)
- A⊂B means A⊆B and A=B (strict containment) Example: {1,2}⊆{1,2} is true; {1,2}⊂{1,2} is false.
Pitfall 4: Misapplying De Morgan's Laws
Mistake: (A∪B)c=Ac∪Bc (WRONG!)
Correct: (A∪B)c=Ac∩Bc and (A∩B)c=Ac∪Bc.
Memory aid: "Break and flip — union becomes intersection, vice versa."
📝 Practice Questions
Q1: Let A={x∣x∈Z, x2≤16}. List A in roster form.Strategy Hint: Find all integers whose square is ≤ 16.Step 1: Integers whose square ≤ 16: (−4)2=16, (−3)2=9, (−2)2=4, (−1)2=1, 02=0, 12=1, 22=4, 32=9, 42=16.Step 2: These are −4,−3,−2,−1,0,1,2,3,4.A={−4,−3,−2,−1,0,1,2,3,4} Q2: If ∣A∣=5, how many proper subsets does A have?Strategy Hint: Proper subsets exclude the set itself.Step 1: Total subsets = 25=32 Step 2: Proper subsets = 32−1=3131 Q3: Let U={1,2,3,4,5,6}, A={1,2,3}, B={2,4,6}. Find (A∪B)c.Strategy Hint: Find the union first, then complement relative to U.Step 1: A∪B={1,2,3,4,6} Step 2: (A∪B)c=U∖(A∪B)={5}{5} Q4: In a group of 60 people, 25 like tea, 30 like coffee, and 10 like both. How many like neither?Strategy Hint: Use inclusion–exclusion.Step 1: ∣T∣=25, ∣C∣=30, ∣T∩C∣=10 Step 2: ∣T∪C∣=25+30−10=45 Step 3: ∣(T∪C)c∣=60−45=1515 people Q5: Write the set of all perfect squares less than 50 in set-builder notation.Strategy Hint: The transform can be applied to the generated variable.{n2∣n∈N, n2<50}Or in roster form: {0,1,4,9,16,25,36,49}. Q6: How many subsets of {a,b,c,d} contain the element a?Strategy Hint: a is fixed as "in". The remaining 3 elements each have 2 choices.Step 1: a is forced to be in every such subset. Step 2: For each of b,c,d: in or out → 23=8 choices.8 Q7: If A⊆B and B⊆C, prove A⊆C.Strategy Hint: Use the definition of subset directly.Proof: Let x∈A. Since A⊆B, x∈B. Since B⊆C, x∈C. Therefore every element of A is in C, so A⊆C. ∎ Q8: Verify De Morgan's law: (A∩B)c=Ac∪Bc for U={1,…,10}, A={2,4,6,8}, B={1,2,3,4,5}.Strategy Hint: Compute both sides independently and compare.Step 1: A∩B={2,4} Step 2: (A∩B)c={1,3,5,6,7,8,9,10} Step 3: Ac={1,3,5,7,9,10}, Bc={6,7,8,9,10} Step 4: Ac∪Bc={1,3,5,6,7,8,9,10} Step 5: Both sides equal {1,3,5,6,7,8,9,10}. ✓Verified Q9: 70 students: 40 play cricket, 35 play football, 20 play both. How many play exactly one sport?Strategy Hint: Exactly one = (Cricket only) + (Football only).Step 1: Cricket only = 40−20=20 Step 2: Football only = 35−20=15 Step 3: Exactly one = 20+15=3535 students Q10: Show that 3 is irrational.Strategy Hint: Follow the same contradiction proof as 2.Proof: Assume 3=p/q in lowest terms. Then p2=3q2. So p2 is divisible by 3 ⇒ p is divisible by 3 (since if a prime divides p2, it divides p). Write p=3k. Then 9k2=3q2⇒q2=3k2, so q is also divisible by 3. Contradiction (gcd would be at least 3). ∎ Q11: Let A={∅,{∅}}. Find P(A).Strategy Hint: A has two elements: ∅ and {∅}.Step 1: ∣A∣=2, so ∣P(A)∣=4. Step 2: P(A)={∅, {∅}, {{∅}}, {∅,{∅}}}{∅, {∅}, {{∅}}, {∅,{∅}}}Note: Don't confuse ∅ (empty set), {∅} (set containing empty set), and {{∅}} (set containing the set containing empty set)! Q12: Among 120 students: 65 enrolled in ML, 55 in AI, 45 in DS, 25 in ML & AI, 20 in ML & DS, 15 in AI & DS, 10 in all three. How many enrolled in at least one?Strategy Hint: Three-set inclusion–exclusion.Step 1: ∣ML∪AI∪DS∣=65+55+45−25−20−15+10 =165−60+10=115115 students
🔗 Cross-References
- Next topic: Relations and Their Properties
- Related: Functions and Their Types — functions are special relations
- Across courses: BSMA1003 Maths 2 (sets form the foundation of linear algebra); BSMA1002 Stats 1 (sample spaces are sets) Join Discord Next1.2 Relations