📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Sets

Sets

4 min read
Sets are unordered collections of unique values: {1, 2, 3}. Use add() and remove() to modify. Set operations union |, intersection &, and difference - work like in math. Great for removing duplicates.

Sets — Unique Values

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

print(a | b)     # union
print(a & b)     # intersection
print(a - b)     # difference
print(a ^ b)     # symmetric diff

a.add(99)
a.discard(1)
print(2 in a)    # True