Python Sets

Let’s look at sets

  1. Notice

    A set contains unique elements. Use membership checks and set operations without relying on an iteration order.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = {3, 3, 2}
print(len(numbers))
print(2 in numbers)
Live state

Top-level scope

numbers→Object 1
Object 1 · set{2, 3}
This step

A set contains unique elements. Use membership checks and set operations without relying on an iteration order.

Picture sets

A set contains unique elements. Use membership checks and set operations without relying on an iteration order.

Image brief

Alt text
A diagram for Python Sets. Instructions: numbers = {3, 3, 2} print(len(numbers)) print(2 in numbers). Displayed result: 2 True. The final example bindings are numbers --> object 1 (set). object 1: {2, 3}
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Sets". Teaching focus: A set contains unique elements. Use membership checks and set operations without relying on an iteration order. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = {3, 3, 2} print(len(numbers)) print(2 in numbers) | v DISPLAYED RESULT 2 True FINAL BINDINGS numbers --> object 1 (set) object 1: {2, 3} Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Sets. Instructions: numbers = {3, 3, 2} print(len(numbers)) print(2 in numbers). Displayed result: 2 True. The final example bindings are numbers --> object 1 (set). object 1: {2, 3}

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = {3, 3, 2}
print(len(numbers))
print(2 in numbers)
Live state

Top-level scope

numbers→Object 1
Object 1 · set{2, 3}
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
2
True

What do you expect this program to print?

What does this example display?

numbers = {3, 3, 2}
print(len(numbers))
print(2 in numbers)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

2 True

Follow the printed values in order. The output is: 2 True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A set contains unique elements. Use membership checks and set operations without relying on an iteration order.

A set contains unique elements. Use membership checks and set operations without relying on an iteration order.

More examples and reference

A Set in Python is a collection of items where:

  • Each item is unique (no duplicates allowed)
  • The items are unordered (they don’t stay in the order you added them)

What Does a Set Look Like?

You can create a set using curly braces {} or the set() function.

fruits = {"apple", "banana", "orange"}
print(fruits)
{'banana', 'orange', 'apple'}

Note: The order might look different when you run it. That’s okay — sets are unordered, so the order is not fixed.

Why Use Sets?

  • To store only unique values
  • To remove duplicates from a list
  • To quickly check if an item exists (sets are fast for lookups)

Removing Duplicates Example

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers)
{1, 2, 3, 4, 5}

Why? Because sets automatically remove duplicates!

Basic Set Operations (CRUD)

Let’s learn the basic operations on sets: Create, Read, Update, and Delete.

1. Create a Set

colors = {"red", "green", "blue"}
print(colors)
{'green', 'blue', 'red'}

2. Read Items

You can’t read by position, but you can check if something exists:

colors = {"red", "green", "blue"}
print("green" in colors)  # True
print("yellow" in colors) # False
True
False

3. Add Items

. Note: order may vary because sets are unordered."
  ]'>
colors = {"red", "green", "blue"}
colors.add("yellow")
print(colors)
{'red', 'green', 'blue', 'yellow'}

4. Remove Items

You can use remove() or discard():

colors = {"red", "green", "blue"}
colors.remove("green")   # Removes green
colors.discard("pink")   # Does nothing (pink not found)
print(colors)
{'red', 'blue'}

5. Clear All Items

colors = {"red", "green", "blue"}
colors.clear()
print(colors)
set()

This means the set is now empty.

Conclusion

Sets are perfect when you want to keep only unique items and don’t care about the order. They are simple, fast, and super useful when removing duplicates or checking membership.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A set contains unique elements. Use membership checks and set operations without relying on an iteration order.

Watch out for this idea

A set guarantees its elements are stored in insertion order.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Functions →