Lesson 1 of 20 · about 13 min
Sets
A set is a collection where order doesn't matter and nothing appears twice. You'll learn the language of sets (members, subsets, union, intersection, difference), see each operation as a picture, and use Python's sets to answer real questions instantly.
🧭 What you need first: Predicates and quantifiers , Dictionaries and sets
Two friends compare music. Ana’s playlist has 40 songs; Ben’s has 35. Which songs could they both enjoy on a road trip? Which songs would only one of them like? How many different songs are there altogether?
Those are set questions. A set is just a collection where we only care what’s in it: not the order, and not how many times something was listed. Once you see collections as sets, questions like “what do these share?” and “what’s missing?” have exact, quick answers.
Sets are everywhere in computing: the users who follow an account, the files that changed since yesterday, the tags on a photo. Databases (F22), search engines and permission systems are all doing set operations, millions of times a second.
Another way: a guest list
A guest list is a set. Writing a name twice doesn’t invite them twice, and the order of names doesn’t matter. “Who’s invited to both parties?” is an intersection.
See the operations
Each circle is a set; the box around them is everything we’re talking about (the universe). Tap an operation to see which region it means. Then switch to Challenges and shade some trickier ones yourself.
Union: everything in Ana, in Ben, or in both.
The real words
- A set is a collection of distinct elements. Write it with braces: .
-
means ” is an element of ”; means it isn’t.
-
Order and repeats don’t matter: .
-
The empty set has no elements. is the size of , its number of elements.
-
Set-builder notation describes a set by a rule (“all such that…”):
Python’s set comprehension
{x for x in range(1, 10) if x % 2 == 0}is the same idea.
-
- Union, intersection, difference:
- : elements in or (or both). Python:
A | B. - : elements in and . Python:
A & B. - : elements in but not in . Python:
A - B. - , the complement: everything in the universe that isn’t in .
- These are the logic connectives from F05 in disguise: means . De Morgan works here too: .
- : elements in or (or both). Python:
- A subset: when every element of is in . Python:
A <= B. Two sets are equal exactly when each is a subset of the other.
Playlist questions
Answer each question with ONE set operation on ana and ben: both_like, only_ana, either (all songs on at least one list), and how_many_total (the number of different songs).
Notice : . The shared songs would be counted twice, so you subtract them once. Lesson 8 turns this into a counting tool.
Build the operations yourself
Without using Python's built-in set operators (| & - <= ^), write union, intersection, difference and is_subset using loops or comprehensions over lists. Each returns a set (is_subset returns True/False).
A = {1, 2, 3, 4} and B = {3, 4, 5}. What is A − B?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?