BeginnerCoder

Lesson 4 — Sets

Sets: maths on groups

A set holds unique items with no order, and checks "is x in here?" instantly. Sets support maths operators: & (intersection: in both), | (union: in either), - (difference: in the first but not the second).

{1, 2, 3} & {2, 3, 4}   # {2, 3}
{1, 2} | {2, 5}         # {1, 2, 5}

We wrap results in sorted() so they always print in the same order.

Why it matters: "which customers bought both?", "who is new this month?" — one operator instead of nested loops.

Want it another way?
Task

Fill in both_clubs, any_club and chess_only with &, | and -. Expected: both ['Cy', 'Dee'].

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.