Skip to the content

Sets

Python · Unit 8: Tuples and sets

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}
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. Multiple choice

    If prizes is a set, why does prizes[0] give an error?

  3. 3. Predict the output

    What does this code print?

  4. 4. Find the bug

    This program should count how many different numbers there are. Which line has the bug?

  5. 5. Complete the code

    Complete it to collect the music genres Emma listens to, without repeats.

  6. 6. Complete the code

    Complete it so it warns you when a number comes up a second time.

  7. 7. Predict the output

    What does this code print?

  8. 8. Predict the output

    What does this code print?

  9. 9. Put the lines in order

    Put in order the function that counts how many different words a sentence has, ignoring capital letters.

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

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 →