Skip to the content

Set operations

Python · Unit 8: Tuples and sets

Amy and Leo collect stickers. a | b is the union: everything in either one, without repeats.

a & b is the intersection: only what's in both.

amy = {1, 2, 3}
leo = {2, 3, 4}
print(amy | leo)
print(amy & leo)

Prints

{1, 2, 3, 4}
{2, 3}

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Complete the code

    Complete it to see which stickers both of them have.

  4. 4. Predict the output

    Which stickers does Leo have that Amy is missing?

  5. 5. Multiple choice

    signed_up has the people who signed up for the workshop and paid has the ones who already paid. Which line gives the people who signed up but haven't paid yet?

  6. 6. Find the bug

    This program should count how many different subjects Amy and Leo take between them. Which line has the bug?

  7. 7. Predict the output

    What does this code print?

  8. 8. Complete the code

    The album has stickers 1 to 10. Complete it so missing returns, sorted, the ones you still don't have.

  9. 9. Find the case that fails

    overlap(a, b) should return True if the two lists have at least one value in common. Which call makes it fail?

  10. 10. Predict the output

    Amy and Leo wrote down what they like. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →