Lesson 16 of 30

Lesson 16 ? Sets and unique things

Title: Keep one of each item

Description: Use sets to remove duplicates and check if something exists.

Why it matters for kids: Sets are useful when you only care if an item is present once.


1. First set

pythonpython
colors = {"red", "blue", "green"}

print(colors)

A set has no fixed order.


2. Duplicates disappear

pythonpython
animals = {"cat", "dog", "cat", "owl"}

print(animals)

cat appears only once.


3. Add items

pythonpython
badges = set()

badges.add("helper")
badges.add("coder")
badges.add("helper")

print(badges)

4. Check membership

pythonpython
collected_keys = {"red", "blue"}

if "red" in collected_keys:
    print("Open the red door.")

5. Easy examples

pythonpython
snacks = {"apple", "cookie", "apple"}
print(snacks)
pythonpython
friends = {"Mia", "Leo"}
friends.add("Sam")
print(friends)
pythonpython
unlocked_levels = {1, 2, 3}
print(4 in unlocked_levels)

6. Mini challenge

Click each example to open it.

Example 1

Create a set of three colors.

Example 2

Print the color set.

Example 3

Add one color to the set.

Example 4

Add the same color twice.

Example 5

Check if red is in the set.

Example 6

Create a set of animals.

Example 7

Remove duplicate animals.

Example 8

Create an empty set.

Example 9

Add two badges to the set.

Example 10

Check if a level is unlocked.