Sets
Python · Unit 8: Tuples and sets
- Full plan
- Python
- 10 exercises
A set stores values without repeats: if you put one in twice, it stays once. It goes inside curly braces {}, like a dictionary but without :.
It has no order and no indexes: Python arranges the values its own way.
nums = {10, 3, 8, 3}
print(nums)
print(len(nums))Prints
{8, 10, 3}
3The 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. Multiple choice
If
prizesis a set, why doesprizes[0]give an error?3. Predict the output
What does this code print?
4. Find the bug
This program should count how many different numbers there are. Which line has the bug?
5. Complete the code
Complete it to collect the music genres Emma listens to, without repeats.
6. Complete the code
Complete it so it warns you when a number comes up a second time.
7. Predict the output
What does this code print?
8. Predict the output
What does this code print?
9. Put the lines in order
Put in order the function that counts how many different words a sentence has, ignoring capital letters.
10. Write the code
Write
no_repeats(nums), which returns a list with the numbers without repeats, in the order they first appeared. Use a set to remember which ones you've already seen.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.