AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Set Theory

📚 Technology⏱️ 17 min read🎓 Grade 8
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 17 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Open the Contacts app on any phone and try to add the same friend twice. You can't — the app quietly refuses, keeping just one copy of "Aarav Sharma." Now think about IRCTC: when you search for trains from Chennai to Bengaluru, the system somehow figures out which trains stop at both stations, ignoring the thousands that touch only one. And when the CBSE result portal shows "students who passed Maths AND Science," it is doing something very precise with two lists of roll numbers. All three of these are the same idea wearing different clothes. That idea is a set — a collection of distinct things — and the rules for combining and comparing sets are called set theory. It is one of the most useful pieces of mathematics a programmer ever learns, because almost every database query, search filter, and "find common items" feature is secretly set theory running underneath.

What exactly is a set?

A set is a collection of objects where two rules are strict and non-negotiable. First, no duplicates: each element appears at most once. Second, order does not matter: the set is defined only by which things are in it, not by the sequence you list them in. That is the whole definition, but those two rules give sets their power.

Let us make it concrete. Suppose the students who signed up for the robotics club are Meera, Rohan, and Priya. We write this set with curly braces:

Robotics = {Meera, Rohan, Priya}

Because order does not matter, {Meera, Rohan, Priya} and {Priya, Meera, Rohan} are the same set. And if a clumsy sign-up sheet listed Rohan twice, the set is still just {Meera, Rohan, Priya} — the second Rohan is invisible. This is exactly why your Contacts app refused the duplicate: a contact list behaves like a set.

The single most important symbol in set theory is , read as "is an element of" or "belongs to." We write Meera ∈ Robotics to say Meera is in the club, and Aarav ∉ Robotics (with a slash) to say Aarav is not. A set answers exactly one question about any object in the world: are you in, or are you out? There is no "how many times" and no "in what position" — just membership, yes or no.

Three ways to describe a set

The roster method simply lists every element: V = {a, e, i, o, u}. This is clear but only works when the set is small enough to write out. For a set like "all even numbers from 2 to 100," listing all fifty would be silly.

The set-builder method describes the elements by a rule instead of listing them. Read the vertical bar | as "such that":

E = { x | x is even and 2 ≤ x ≤ 100 }

Out loud this is: "E is the set of all x such that x is even and x is between 2 and 100." The rule does the work of listing. This is the same thinking a programmer uses in a Python set comprehension — notice how close the code is to the maths:

E = { x for x in range(2, 101) if x % 2 == 0 }
print(len(E))   # 50

The third idea is the universal set, written U: the full collection of everything we are currently talking about. If our discussion is about a single dice roll, then U = {1, 2, 3, 4, 5, 6}. The universal set matters because "everything NOT in A" only makes sense once we agree on what "everything" is.

Special sets and set size

Two special sets show up constantly. The empty set, written or {}, is the set with no elements at all — the set of "students who scored above 100 out of 100" is empty. It is not nothing; it is a real, valid set that happens to be empty, just as an empty cricket stadium is still a stadium.

The number of elements in a set is its cardinality, written with vertical bars. If A = {2, 4, 6, 8, 10} then |A| = 5. For the empty set, |∅| = 0. Cardinality is just a fancy word for "how big is this set," and because sets ignore duplicates, cardinality counts distinct elements only.

Subsets: sets living inside sets

Set A is a subset of set B — written A ⊆ B — when every single element of A is also in B. Think of it as "A fits entirely inside B." The set of vowels is a subset of the set of all letters. The set {2, 4} is a subset of {2, 4, 6, 8, 10} because both 2 and 4 live in the bigger set. But {2, 5} is not a subset, because 5 breaks the rule — one outsider is enough to disqualify it.

A useful and slightly surprising fact: the empty set is a subset of every set. Why? To be a subset, every element of ∅ must be in B — and since ∅ has no elements, there is no element that could fail the test. The condition is satisfied for free. Students often find this weird, so hold onto the phrasing: a subset relationship can only be broken by an element that is inside but outside; the empty set never supplies such a troublemaker.

The four core operations — with a worked example

Almost everything interesting in set theory comes from four operations. Let us fix two sets and compute all four by hand, then check with code. Take:

A = {2, 4, 6, 8, 10}
B = {3, 6, 9}

Union (A ∪ B) — "in A OR in B (or both)." Pour both sets into one bag and remove duplicates. Walk through every element: from A we take 2, 4, 6, 8, 10; from B we add 3 and 9 (6 is already there, so we do not add it again). Result: {2, 3, 4, 6, 8, 9, 10}. This is the IRCTC "show me trains that touch either station" logic.

Intersection (A ∩ B) — "in A AND in B." Keep only the elements that appear in both. Checking each element of A against B: 2? no. 4? no. 6? yes — it is in B too. 8? no. 10? no. Only 6 survives, so A ∩ B = {6}. This is the "passed Maths AND Science" logic, and the "trains that stop at both stations" logic.

Difference (A − B) — "in A but NOT in B." Start with A and throw out anything that is also in B. From {2, 4, 6, 8, 10} we remove 6 (the only shared element), leaving {2, 4, 8, 10}. Difference is not symmetric — direction matters. Going the other way, B − A starts with B and removes the shared 6, giving {3, 9}. Two different answers, so always read the order carefully.

Complement (A′) — "everything in the universal set that is NOT in A." Suppose U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. Then A′ is U minus A: {1, 3, 5, 7, 9}. Complement is really just "difference from the universe," which is why we insisted earlier that U be pinned down first.

Here is the Venn diagram for A and B. The overlapping region is the intersection; the whole coloured area together is the union.

U = {1 … 10} A B 2 4 8 10 6 3 9 Overlap = A ∩ B = {6}

Now the code. Python has sets as a first-class type, and the operators read almost exactly like the maths — | for union, & for intersection, - for difference:

A = {2, 4, 6, 8, 10}
B = {3, 6, 9}
U = set(range(1, 11))        # {1,2,3,4,5,6,7,8,9,10}

print(sorted(A | B))   # [2, 3, 4, 6, 8, 9, 10]   union
print(sorted(A & B))   # [6]                       intersection
print(sorted(A - B))   # [2, 4, 8, 10]             difference
print(sorted(B - A))   # [3, 9]                     other direction
print(sorted(U - A))   # [1, 3, 5, 7, 9]           complement of A

Every printed line matches the hand-computed answers above. We wrap each result in sorted(...) only so the display order is predictable — remember, the set itself has no order, so sorted is purely for our human eyes.

A misconception worth correcting: "union just means add the sizes"

A very common mistake is to think |A ∪ B| = |A| + |B| — that the size of a union is just both sizes added. Test it: |A| = 5 and |B| = 3, so that formula predicts 8. But the actual union {2, 3, 4, 6, 8, 9, 10} has only 7 elements. Where did the missing one go? The element 6 belongs to both sets, and if you add 5 + 3 you have counted 6 twice. To fix the count you subtract the overlap once:

|A ∪ B| = |A| + |B| − |A ∩ B|
        =   5   +   3   −      1        = 7   ✓

This is the inclusion–exclusion principle, and it is genuinely useful. Imagine a class where 22 students play cricket, 18 play badminton, and 7 play both. How many play at least one sport? Not 40 — that double-counts the 7 who do both. The answer is 22 + 18 − 7 = 33. Any time you hear "and 7 do both," your brain should reach for the subtraction.

The power set — every possible subset

Here is where set theory starts to feel deep. Given a set, its power set is the set of all its subsets, including the empty set and the whole set itself. Take the tiny set S = {a, b, c}. Its subsets are:

{ }, {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c}

Count them: 8 subsets. Notice the pattern — a set with 3 elements has 8 = 2³ subsets. This is not a coincidence. To build any subset, you make one yes/no choice per element: "is a in? is b in? is c in?" Three independent choices, each with 2 options, gives 2 × 2 × 2 = 2³ = 8. In general a set with n elements has exactly 2ⁿ subsets. A set of 10 elements already has 1024 subsets — this explosive growth is why "try every possible combination" is often too slow for computers, and it is a first glimpse of why some problems are genuinely hard.

Sets in the real machine

Programmers reach for sets constantly, and for a concrete reason beyond elegance: membership testing is fast. Asking "is roll number 4071 in this set?" takes roughly the same tiny amount of time whether the set has 10 elements or 10 million, because sets are built on a structure called a hash table. Checking membership in an ordinary list, by contrast, means scanning element by element and gets slower as the list grows. So when IRCTC needs to know whether a station lies on a train's route, or when a spam filter checks whether a sender is on a blocklist, a set is the natural tool.

Deduplication is the other everyday use. Suppose a survey collected phone numbers with plenty of repeats. Converting the list to a set instantly discards duplicates because a set cannot hold the same element twice:

numbers = [98401, 77220, 98401, 63550, 77220, 98401]
unique = set(numbers)
print(len(unique))   # 3   — only 98401, 77220, 63550 remain

One short line replaces a fiddly loop, and it works precisely because of the no-duplicates rule we started with.

Active recall — try these before reading the answers

Do not just read these; actually work them on paper. Let P = {1, 2, 3, 4, 5, 6} and Q = {4, 5, 6, 7, 8}, with universal set U = {1, 2, …, 10}.

  1. Write P ∪ Q, P ∩ Q, P − Q, and Q − P by hand.
  2. Use inclusion–exclusion to predict |P ∪ Q| without listing, then verify by counting your union from question 1.
  3. Find P′ (the complement of P). Which elements of U are left?
  4. Is {4, 6} ⊆ Q? Is {6, 7} ⊆ P? Explain each in one sentence.
  5. How many subsets does Q have? (It has 5 elements.)
  6. In a colony, 30 homes have a UPI-linked account, 24 have a fixed deposit, and 10 have both. How many homes have at least one of the two?

Answers. (1) P ∪ Q = {1,2,3,4,5,6,7,8}; P ∩ Q = {4,5,6}; P − Q = {1,2,3}; Q − P = {7,8}. (2) |P| + |Q| − |P ∩ Q| = 6 + 5 − 3 = 8, and the union indeed has 8 elements. (3) P′ = {7, 8, 9, 10}. (4) Yes, both 4 and 6 are in Q; no, because 7 is not in P, and one outsider breaks the subset. (5) 2⁵ = 32 subsets. (6) 30 + 24 − 10 = 44 homes.

Key ideas to carry forward

A set is a collection with no duplicates and no order, and its only question is membership (∈ or ∉). Describe sets by roster or by a set-builder rule; the special empty set ∅ is a subset of everything, and cardinality |A| counts distinct elements. The four operations — union (OR), intersection (AND), difference (in one but not the other), and complement (everything outside, relative to U) — cover almost every real query you will meet. When counting a union, subtract the overlap so you do not double-count: |A ∪ B| = |A| + |B| − |A ∩ B|. A set of n elements has 2ⁿ subsets, its power set. And in real code, sets give you near-instant membership tests and one-line deduplication. Master these, and database queries, search filters, and a surprising amount of algorithmic thinking will suddenly look like old friends.

Practice Exercises

Now it is time to practice! Complete these challenges to solidify your understanding:

  • Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
  • Exercise 2: Find a real-world example where set theory is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
  • Exercise 3: Create a mind-map connecting set theory to at least 3 other topics you have studied.

Key Takeaways — Summary and Recap

Let us recap what we covered: the core ideas behind set theory, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.

← ProbabilityBoolean Algebra: The Logic Behind Computing →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn