Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Sets: union, intersection and difference

After this lesson: I can combine sets and count their members.

50 min lesson3 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Who is in the chess or robotics club?

A set is a collection of different things, such as the students in a club. Set operations tell you who is in one club, in both, or in neither.

A lecturer must book rooms for two clubs, and some students belong to both. Counting them twice books too many rooms. This lesson shows how to combine sets and count their members correctly.

By the end of this lesson

Note

I can combine sets and count their members.

Key words

WordWhat it means
Seta collection of different things, in no order
Elementone thing inside a set
Unioneverything that is in either set
Intersectioneverything that is in both sets
Differencewhat is in the first set but not the second
Sizehow many elements a set has

The cells students fill in are left blank in this preview.

Union, intersection and difference combine sets

Say the chess club has Ana, Ben, Cai and Dee. The robotics club has Cai, Dee, Eli and Fay.

The union is everyone in either club. The intersection is everyone in both. The difference "chess minus robotics" is everyone in chess who is not in robotics. These match or, and, and "and not" from logic.

Worked example: eight students, two clubs

StudentChessRoboticsUnion (either)Intersection (both)Chess minus robotics
AnaYesNoYesNoYes
BenYesNoYesNoYes
CaiYesYesYesYesNo
DeeYesYesYesYesNo
EliNoYesYesNoNo
FayNoYesYesNoNo
GusNoNoNoNoNo
HalNoNoNoNoNo

The cells students fill in are left blank in this preview.

Let Python combine the two clubs

Command
python3 -c "c = {'Ana', 'Ben', 'Cai', 'Dee'}; r = {'Cai', 'Dee', 'Eli', 'Fay'}; print(sorted(c | r), sorted(c & r), sorted(c - r))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Add the sizes, then take away the overlap

How many students are in at least one club? Do not add 4 and 4. Cai and Dee are in both lists, so adding counts them twice.

Take the overlap away once: 4 + 4 - 2 = 6. This is called inclusion and exclusion. You include both sets, then exclude the double count.

Worked example: an overlap of 5

StepCount
Students in chess18
Students in robotics12
Add them30
Students in both, counted twice5
Take the overlap away once30 - 5 = 25

The cells students fill in are left blank in this preview.

Count the union by program

Command
python3 -c "a = set(range(1, 19)); b = set(range(14, 26)); print(len(a), len(b), len(a & b), len(a | b))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think the size of a union is the two sizes added. That is true only when the sets share nothing. When they overlap, the shared elements are counted twice, so take the size of the intersection away once.

A set ignores order and repeats

A list can hold the same value twice and keeps its order. A set cannot and does not. The set {3, 1, 3, 2} is just {1, 2, 3}.

A set A is a subset of B when every element of A is also in B.

Turn a list into a set

Command
python3 -c "print(sorted(set([3, 1, 3, 2, 1])), {1, 2} <= {1, 2, 3})"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Try it: find the overlap with a loop

Code: python
chess = [1, 2, 3, 4, 5, 6]
robotics = [5, 6, 7, 8]
both = []
for name in chess:
    if name in robotics:
        both.append(name)
print("in both:", both)
print("in either:", len(chess) + len(robotics) - len(both))
# Add 9 to robotics and press Run again. What changes?

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.