Set operations
Python · Unit 8: Tuples and sets
- Full plan
- Python
- 10 exercises
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. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Complete the code
Complete it to see which stickers both of them have.
4. Predict the output
Which stickers does Leo have that Amy is missing?
5. Multiple choice
signed_uphas the people who signed up for the workshop andpaidhas the ones who already paid. Which line gives the people who signed up but haven't paid yet?6. Find the bug
This program should count how many different subjects Amy and Leo take between them. Which line has the bug?
7. Predict the output
What does this code print?
8. Complete the code
The album has stickers 1 to 10. Complete it so
missingreturns, sorted, the ones you still don't have.9. Find the case that fails
overlap(a, b)should returnTrueif the two lists have at least one value in common. Which call makes it fail?10. Predict the output
Amy and Leo wrote down what they like. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.