Skip to content
DoingCamp CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

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

The story

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.

Play with it

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.

AnaBen

Union: everything in Ana, in Ben, or in both.

Name it

The real words

  • A set is a collection of distinct elements. Write it with braces: {2,3,5,7}\{2, 3, 5, 7\}.
    • x∈Ax \in A means ”xx is an element of AA”; x∉Ax \notin A means it isn’t.

    • Order and repeats don’t matter: {1,2,2,3}={3,2,1}\{1, 2, 2, 3\} = \{3, 2, 1\}.

    • The empty set ∅\varnothing has no elements. ∣A∣|A| is the size of AA, its number of elements.

    • Set-builder notation describes a set by a rule (“all xx such that…”):

      {x∣x is even, 0<x<10}={2,4,6,8}\{x \mid x \text{ is even},\ 0 < x < 10\} = \{2, 4, 6, 8\}

      Python’s set comprehension {x for x in range(1, 10) if x % 2 == 0} is the same idea.

  • Union, intersection, difference:
    • A∪BA \cup B: elements in AA or BB (or both). Python: A | B.
    • A∩BA \cap B: elements in AA and BB. Python: A & B.
    • A−BA - B: elements in AA but not in BB. Python: A - B.
    • AcA^c, the complement: everything in the universe that isn’t in AA.
    • These are the logic connectives from F05 in disguise: x∈A∪Bx \in A \cup B means x∈A∨x∈Bx \in A \lor x \in B. De Morgan works here too: (A∪B)c=Ac∩Bc(A \cup B)^c = A^c \cap B^c.
  • A subset: A⊆BA \subseteq B when every element of AA is in BB. Python: A <= B. Two sets are equal exactly when each is a subset of the other.
Build it

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 ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|: 6+5−3=86 + 5 - 3 = 8. The shared songs would be counted twice, so you subtract them once. Lesson 8 turns this into a counting tool.

Challenge

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).

Remember it

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?