A set is a collection with no order and no repeats. Learn to write sets down, to combine two of them by union, intersection, difference and complement, to see each result as a region of a Venn diagram, and to check De Morgan’s laws region by region.
Write a set by listing its elements and in set-builder notation, and decide whether one set is a subset of another.
Find the union, intersection, difference and complement of two sets inside a universe, and shade each one on a Venn diagram.
Check De Morgan’s two laws on the four regions of a Venn diagram, and explain why that check covers every pair of sets.
Combine sets with Python’s set type, and test that a training set and a test set share no example.
In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.
The question
A model learns from one group of examples, the training set, and is then scored on another, the test set. If even one example sits in both groups, the score flatters the model, because it has already seen that answer. What exactly do “in both”, “in either” and “in neither” mean, and how do you check them with one line of code?
Intuition
A set is a collection of things, called its elements. Two rules make a set different from a list: the order does not matter, and nothing is counted twice. The even numbers from 1 to 8 form the same set whether you write them 2, 4, 6, 8 or 8, 6, 4, 2.
This module uses one small example all the way through. The universe\({U}\) is the set of everything under discussion, here the numbers 1 to 8. Inside it sit two sets:
\({A}\), the even numbers: 2, 4, 6 and 8;
\({B}\), the numbers up to 4: 1, 2, 3 and 4.
Draw the universe as a rectangle and each set as a circle. The circles overlap, because 2 and 4 belong to both sets. This picture is called a Venn diagram.
M01.6.1-F1
The course’s Venn diagram. Two overlapping circles cut the universe \({U = \{1, \ldots, 8\}}\) into four regions, and each region here holds exactly two of the eight numbers.
Every number of the universe lands in exactly one of the four regions, because it answers two yes-or-no questions: is it in \({A}\)? Is it in \({B}\)? Two questions with two answers each give four combinations, and each combination is one region.
Everything this lesson does to sets is a choice of regions: “in \({A}\) or in \({B}\)” is three of them, and “in both” is the middle one.
Build it up
Step 1. List the elements. Write them between curly brackets, called braces: \({A = \{2, 4, 6, 8\}}\). Neither order nor repeats matter, so \({\{8, 6, 4, 2\}}\) is the same set. We write \({6 \in A}\), read “6 is in \({A}\)”, and \({5 \notin A}\), read “5 is not in \({A}\)”.
Step 2. Describe the elements by a rule. A long or endless set is easier to describe than to list. Set-builder notation writes a rule after a colon, which is read “such that”:
\[
B = \{x \in U : x \le 4\}.
\]
Read it as “the set of \({x}\) in \({U}\) such that \({x}\) is at most 4”. Some books write a vertical bar for the colon; this course keeps the bar for conditional probability.
Step 3. Count the elements. The size of a finite set is the number of its elements, written with the bars of an absolute value: \({\abs{A} = 4}\). The set with no elements is the empty set, \({\emptyset}\), and \({\abs{\emptyset} = 0}\). A rule that nothing obeys gives the empty set: no number of \({U}\) is bigger than 8, so
\[
\{x \in U : x > 8\} = \emptyset.
\]
Step 4. Compare two sets. A set \({C}\) is a subset of a set \({D}\), written \({C \subseteq D}\), when every element of \({C}\) is also in \({D}\). So \({\{2, 4\} \subseteq A}\) and \({A \subseteq U}\), but \({A}\) is not a subset of \({B}\), because \({6 \in A}\) and \({6 \notin B}\). Every set is a subset of itself, and the empty set is a subset of every set: to fail, it would need an element missing from the other set, and it has none. Two sets are equal when each is a subset of the other.
Step 5. See the number systems as sets. The kinds of numbers of M01.1.1 are four sets, each inside the next. The natural numbers are \({\mathbb{N} = \{1, 2, 3, \ldots\}}\) (this course starts them at 1). The integers\({\mathbb{Z}}\) add zero and the negatives. The rational numbers\({\mathbb{Q}}\) are the fractions \({p/q}\) of integers with \({q \ne 0}\). The real numbers\({\R}\) are all the points of the number line.
M01.6.1-F2
The number systems as nested sets. Each number between two boxes belongs to the outer set but not to the inner one.
The square root of 2 sits in \({\R}\) but outside \({\mathbb{Q}}\): no fraction squares to exactly 2, as lesson M01.6.4 proves. So does \({\pi}\), though its proof is beyond this course.
Step 6. Make new sets from old. Four operations each pick out some regions of the diagram. The union\({A \cup B}\) holds everything in \({A}\) or in \({B}\). The intersection\({A \cap B}\) holds everything in both. The difference\({A - B}\) holds what is in \({A}\) but not in \({B}\). The complement\({A^c}\) holds what is in \({U}\) but not in \({A}\).
M01.6.1-F3
The four operations as regions. The result of each one is shaded, and its numbers are in bold.
Two sets with no element in common are disjoint: their intersection is empty. That answers the opening question: a training set and a test set must be disjoint.
Let \({A}\) and \({B}\) be sets inside a universe \({U}\).
The union\({A \cup B}\) is the set of elements that are in \({A}\) or in \({B}\) (or in both).
The intersection\({A \cap B}\) is the set of elements that are in \({A}\) and in \({B}\).
The difference\({A - B}\) is the set of elements that are in \({A}\) and not in \({B}\).
The complement\({A^c}\) is the set of elements of \({U}\) that are not in \({A}\), so \({A^c = U - A}\).
In set-builder notation the first two read
\[
\begin{aligned}
A \cup B &= \{x : x \in A \text{ or } x \in B\}, \\
A \cap B &= \{x : x \in A \text{ and } x \in B\}.
\end{aligned}
\]
The “or” of the union includes “both”: 2 is in \({A \cup B}\). The complement depends on the universe: with \({U = \{1, \ldots, 10\}}\), \({A^c}\) would also hold 9 and 10. Other books write \({A \setminus B}\) for the difference, and an overbar or a prime for the complement; this course keeps the overbar for means.
NoteDe Morgan’s laws for sets
For any two sets \({A}\) and \({B}\) inside a universe \({U}\),
\[
\begin{aligned}
(A \cup B)^c &= A^c \cap B^c, \\
(A \cap B)^c &= A^c \cup B^c.
\end{aligned}
\]
In words: the complement of a union is the intersection of the complements, and the complement of an intersection is the union of the complements. Taking the complement swaps \({\cup}\) and \({\cap}\).
Proof, region by region. Every element of \({U}\) lies in exactly one of the four regions: only \({A}\), both, only \({B}\), neither. Whether an element belongs to either side of a law depends only on its region, so each side is made of whole regions, and the two sides are equal exactly when they are made of the same regions.
First law. The union \({A \cup B}\) is made of the regions only \({A}\), both and only \({B}\), so its complement is the one region left over: neither. On the other side, \({A^c}\) is made of only \({B}\) and neither, and \({B^c}\) is made of only \({A}\) and neither. Their intersection keeps the regions on both lists: just neither. The two sides are the same region.
Second law. The intersection \({A \cap B}\) is the region both, so its complement is made of the other three: only \({A}\), only \({B}\) and neither. On the other side, \({A^c \cup B^c}\) joins the two lists above, which gives only \({B}\), neither and only \({A}\): the same three regions. \({\blacksquare}\)
The argument never used the numbers 1 to 8, so the laws hold for every pair of sets.
Go deeper: why four regions settle every question about two sets
An element’s region is just its pair of answers to “in \({A}\)?” and “in \({B}\)?”, and each operation of this lesson decides membership from those answers alone. So any set built from \({A}\) and \({B}\) with these operations, however long its formula, is a collection of whole regions. Checking all four regions is therefore a complete proof, an example of proof by cases, which lesson M01.6.3 returns to. Three sets would make eight regions.
Worked example
Check both of De Morgan’s laws on the running example, first with the numbers and then region by region.
First law, left side. The union is \({A \cup B = \{1, 2, 3, 4, 6, 8\}}\). Its complement holds the numbers of \({U}\) that the union misses, so \({(A \cup B)^c = \{5, 7\}}\).
First law, right side. The complements are \({A^c = \{1, 3, 5, 7\}}\) and \({B^c = \{5, 6, 7, 8\}}\). The numbers on both lists are 5 and 7, so \({A^c \cap B^c = \{5, 7\}}\). The two sides agree.
Second law, left side. The intersection is \({A \cap B = \{2, 4\}}\), so its complement is \({(A \cap B)^c = \{1, 3, 5, 6, 7, 8\}}\).
Second law, right side. Joining \({\{1, 3, 5, 7\}}\) and \({\{5, 6, 7, 8\}}\), and writing 5 and 7 only once, gives \({A^c \cup B^c = \{1, 3, 5, 6, 7, 8\}}\). The two sides agree again.
Now the same check region by region. Each row below is one region with its two numbers, and a filled dot means the region is inside that set.
M01.6.1-F4
The worked-example trace. In each law the two columns agree on every row, so the two sides are made of the same regions.
Agreement on the numbers covers this one pair of sets; agreement row by row covers every pair, because every pair has these four regions.
Code
First from scratch, with each set kept as a Python list. A list may hold repeats, so the union skips any number it has already kept.
U = [1, 2, 3, 4, 5, 6, 7, 8]A = [2, 4, 6, 8] # evenB = [1, 2, 3, 4] # up to 4def union(X, Y): out = []# X + Y joins the two listsfor x in X + Y:# skip repeatsif x notin out: out.append(x)# sorted() puts in orderreturnsorted(out)def intersection(X, Y): out = []for x in X:# keep x if Y has it tooif x in Y: out.append(x)return outdef complement(X): out = []# what U has and X lacksfor x in U:if x notin X: out.append(x)return outprint("union:", union(A, B))print("intersection:", intersection(A, B))print("(A union B)^c:", complement(union(A, B)))Ac = complement(A)Bc = complement(B)print("A^c intersect B^c:", intersection(Ac, Bc))
union: [1, 2, 3, 4, 6, 8]
intersection: [2, 4]
(A union B)^c: [5, 7]
A^c intersect B^c: [5, 7]
Python has sets built in. Braces make a set, and operators do the work of the functions above: | for union, & for intersection, - for difference and <= for “is a subset of”. A set has no order, so each result goes through sorted before it is printed.
A = {2, 4, 6, 8}B = {1, 2, 3, 4}# range(1, 9) is 1, 2, ..., 8U =set(range(1, 9))print("A | B:", sorted(A | B))print("A & B:", sorted(A & B))print("A - B:", sorted(A - B))print("B - A:", sorted(B - A))# U - A is the complement of Aprint("U - A:", sorted(U - A))print("6 in A:", 6in A)print("5 in A:", 5in A)print("size of A:", len(A))print("{2, 4} <= A:", {2, 4} <= A)print("A <= B:", A <= B)# De Morgan's two lawslaw1 = (U - (A | B)== (U - A) & (U - B))law2 = (U - (A & B)== (U - A) | (U - B))print("first law holds:", law1)print("second law holds:", law2)
A | B: [1, 2, 3, 4, 6, 8]
A & B: [2, 4]
A - B: [6, 8]
B - A: [1, 3]
U - A: [1, 3, 5, 7]
6 in A: True
5 in A: False
size of A: 4
{2, 4} <= A: True
A <= B: False
first law holds: True
second law holds: True
Now the opening question. Number eight examples 1 to 8 and split them. The first split puts example 6 in both groups, and the intersection finds it at once.
shared: [6]
disjoint: True
disjoint: True
all used: True
set() is the empty set, and isdisjoint asks the same question. The last line checks that the split loses nothing: together the two groups make up the whole data set.
Frontier note: intersection over union, a score built from two sets
A model that tags a photo with several labels can be scored by comparing its set of labels with the true set: the size of the intersection divided by the size of the union. This is the Jaccard index, computed by scikit-learn’s jaccard_score. For the running example it is \({2/6 \approx 0.3333}\). When the two sets are not both empty, it is 1 for equal sets and 0 for disjoint ones.
Explore
The module’s Venn and truth-table explorer is in the next lesson, but its Venn half needs only this one: pick \({(A \cup B)^c}\), or tap regions, and watch the numbers 1 to 8 sort into the set and out of it.
Pitfalls
M01.6.1-F5
Wrong beside right: complementing each set is right, but keeping the union sign is not.
“The complement of \({A \cup B}\) is \({A^c \cup B^c}\).”
Taking the complement of each set is right; keeping \({\cup}\) is wrong. Here \({A^c \cup B^c}\) holds six numbers, while \({(A \cup B)^c}\) holds only 5 and 7. The complement swaps the operation: \({\cup}\) becomes \({\cap}\), and \({\cap}\) becomes \({\cup}\). The same slip in a data filter, met in the next lesson, silently drops rows.
“\({A - B}\) and \({B - A}\) are the same set.”
Union and intersection ignore order, but difference does not. Here \({A - B = \{6, 8\}}\) while \({B - A = \{1, 3\}}\): the two outer regions of the diagram, on opposite sides.
“2 and {2} are the same thing.”
2 is a number, and \({\{2\}}\) is a set whose one element is that number. So \({2 \in A}\) and \({\{2\} \subseteq A}\) are true, while \({\{2\} \in A}\) is false: the elements of \({A}\) are numbers, not sets. In code, too, a list holding one number is not that number.
“In Python, {} is the empty set.”
Empty braces make an empty dictionary. So (train & test) == {} is always False, even for disjoint sets, because a set never equals a dictionary. Write set(), or ask train.isdisjoint(test).
Check yourself
Recall
1. Write \({B = \{1, 2, 3, 4\}}\) in set-builder notation, with \({U = \{1, \ldots, 8\}}\).
Show answer
\({B = \{x \in U : x \le 4\}}\), read “the set of \({x}\) in \({U}\) such that \({x}\) is at most 4”.
2. Why is the empty set a subset of every set?
Show answer
To fail to be a subset, it would need an element that is missing from the other set, and it has no elements at all.
3. On the running example, what are \({A \cap B}\) and \({A - B}\)?
Show answer
\({A \cap B = \{2, 4\}}\), the overlap, and \({A - B = \{6, 8\}}\), the part of \({A}\) outside \({B}\).
4. What is \({A^c}\), and how would it change if the universe were \({\{1, \ldots, 10\}}\)?
Show answer
\({A^c = \{1, 3, 5, 7\}}\). With the larger universe, 9 and 10 would join it, because the complement holds everything in the universe that is not in \({A}\).
5. State De Morgan’s two laws in words.
Show answer
The complement of a union is the intersection of the complements, and the complement of an intersection is the union of the complements.
Exercises
1.★ Let \({U = \{1, \ldots, 10\}}\) and \({D = \{1, 2, 3, 4, 5\}}\), and let \({C}\) hold the multiples of 3 in \({U}\). List \({C}\). Then find \({C \cup D}\), \({C \cap D}\), \({C - D}\), \({D - C}\) and \({C^c}\), and give \({\abs{C \cup D}}\).
Show solution
The multiples of 3 up to 10 are 3, 6 and 9, so \({C = \{3, 6, 9\}}\). Then
\({C \cup D = \{1, 2, 3, 4, 5, 6, 9\}}\), so \({\abs{C \cup D} = 7}\);
\({C \cap D = \{3\}}\);
\({C - D = \{6, 9\}}\) and \({D - C = \{1, 2, 4, 5\}}\);
\({C^c = \{1, 2, 4, 5, 7, 8, 10\}}\).
2.★★ Ten examples, numbered 1 to 10, are split into a training set \({T = \{1, 2, 3, 4, 5, 6, 7\}}\), a validation set \({V = \{7, 8\}}\) and a test set \({S = \{9, 10\}}\). (a) Which pairs of these sets are disjoint? (b) Which example is used twice, and how would you repair the split? (c) Check that after your repair the three sets together still hold all ten examples.
Show solution
(a) \({T \cap S}\) and \({V \cap S}\) are empty, so those pairs are disjoint. But \({T \cap V = \{7\}}\).
(b) Example 7 is in both \({T}\) and \({V}\). Take it out of one of them, for instance \({T = \{1, 2, 3, 4, 5, 6\}}\). Now every pair is disjoint.
(c) Joining \({\{1, \ldots, 6\}}\), \({\{7, 8\}}\) and \({\{9, 10\}}\) gives \({\{1, \ldots, 10\}}\). No example is lost.
3.★★★ Show, region by region, that the two sets below are equal for any sets \({A}\) and \({B}\) in a universe \({U}\). Then use this and De Morgan’s second law to find a second way to write \({(A - B)^c}\), and check it on the running example.
\[
A - B = A \cap B^c
\]
Show solution
\({A - B}\) is the region only \({A}\). For the other side, \({A}\) is made of only \({A}\) and both, and \({B^c}\) is made of only \({A}\) and neither; the region on both lists is only \({A}\). The two sides are the same region, so they are equal.
Now take complements, using De Morgan’s second law with \({B^c}\) in the place of \({B}\). Taking the complement twice gives back the regions of \({B}\), so \({(B^c)^c = B}\), and
\[
\begin{aligned}
(A - B)^c &= (A \cap B^c)^c \\
&= A^c \cup (B^c)^c \\
&= A^c \cup B.
\end{aligned}
\]
Check: \({A - B = \{6, 8\}}\), so \({(A - B)^c = \{1, 2, 3, 4, 5, 7\}}\). And joining \({A^c = \{1, 3, 5, 7\}}\) with \({B = \{1, 2, 3, 4\}}\) also gives \({\{1, 2, 3, 4, 5, 7\}}\). They agree.
Summary card
Two sets cut the universe into four regions, and every operation is a choice of regions.
M01.6.1-F1
Four regions, two numbers in each.
Union\({A \cup B}\): in \({A}\) or in \({B}\). Intersection\({A \cap B}\): in both.
Difference\({A - B}\): in \({A}\), not in \({B}\). Complement\({A^c}\): in \({U}\), not in \({A}\).
Python.A | B, A & B, A - B, U - A, x in A, len(A), A <= B, and set() for \({\emptyset}\).
De Morgan’s laws. Taking the complement swaps \({\cup}\) and \({\cap}\):
\[
\begin{aligned}
(A \cup B)^c &= A^c \cap B^c \\
(A \cap B)^c &= A^c \cup B^c
\end{aligned}
\]
Sources and next steps
Sources. Each was opened and checked against this lesson on 12 November 2026.
Richard Hammack, Book of Proof, third edition (free PDF). Section 1.1: sets, elements, \({\mathbb{N} = \{1, 2, 3, \ldots\}}\), the size \({\abs{X}}\), the empty set, and the colon of set-builder notation read “such that”. Section 1.3: subsets (Definition 1.3), the empty set inside every set (Fact 1.2) and the chain of number systems. Sections 1.5 to 1.7: union, intersection and difference (Definition 1.5), the complement (Definition 1.6, written with an overbar) and Venn diagrams. Section 8.3 lists De Morgan’s laws for sets.
Jay Abramson and others, Algebra and Trigonometry 2e (OpenStax), Section 1.1: Figure 2 puts \({\pi}\) with \({\sqrt{2}}\) among the irrational numbers.
Donna Kirk, Contemporary Mathematics (OpenStax). Section 1.4 defines union, intersection and complement (written \({A'}\)), and Section 1.5, Example 1.38, checks De Morgan’s first law by shading Venn diagrams.
Python Software Foundation, The Python Tutorial, Section 5.4 Sets: set(), not {}, makes an empty set, and |, & and - are union, intersection and difference. Set Types gives isdisjoint and the subset test <=.
scikit-learn 1.9.1, jaccard_score: the size of the intersection divided by the size of the union of two label sets.
Next.M01.6.2 Statements and logic finds the four regions again as the four rows of a truth table. Counting the elements of a union waits for M01.8.1.