🎯
Python Sets
Unordered collections of unique items
Reward
+40 XP
Creating sets
Sets hold unique items only. Duplicates are automatically removed.
s = {1, 2, 3, 2, 1}
print(s) # {1, 2, 3}Set operations
Union, intersection, and difference let you combine sets.
a = {1, 2, 3}
b = {2, 3, 4}
print(a | b) # union
print(a & b) # intersection
print(a - b) # differenceTry it yourself
Run the example below. Edit it and see what changes.
main.py
Output
Press Run to execute (Ctrl/Cmd+Enter)
Common mistakes
- ⚠Sets are unordered — you cannot access items by index.
- ⚠Use set() for an empty set, not {} (that creates a dict).
