Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
numbers = {3, 3, 2}
print(len(numbers))
print(2 in numbers)Top-level scope
{2, 3}A set contains unique elements. Use membership checks and set operations without relying on an iteration order.
(no output)
What do you expect this program to print?
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}Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
numbers = {3, 3, 2}
print(len(numbers))
print(2 in numbers)Top-level scope
{2, 3}The example has finished. Compare the final output with the values and references above.
2 True
What do you expect this program to print?
numbers = {3, 3, 2}
print(len(numbers))
print(2 in numbers)A Set in Python is a collection of items where:
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.
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!
Let’s learn the basic operations on sets: Create, Read, Update, and Delete.
colors = {"red", "green", "blue"}
print(colors)
{'green', 'blue', 'red'}
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
. Note: order may vary because sets are unordered."
]'>
colors = {"red", "green", "blue"}
colors.add("yellow")
print(colors)
{'red', 'green', 'blue', 'yellow'}
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'}
colors = {"red", "green", "blue"}
colors.clear()
print(colors)
set()
This means the set is now empty.
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.
Connect what you noticed in the example with the idea below.
A set contains unique elements. Use membership checks and set operations without relying on an iteration order.
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.