Lesson 16 of 55
PythonData Structures – Sets
20 minutes
Course progress29%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 16 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Sets store unique, unordered items and support mathematical operations like union, intersection, and difference.
primes = {2, 3, 5, 7}
primes.add(11)
primes.update([13, 17])
print('All primes:', primes)
# Set operations
even = {2, 4, 6, 8}
print('Intersection:', primes & even) # {2}
print('Union:', primes | even) # all unique numbers
Because sets discard duplicates automatically, they’re great for membership testing (x in my_set).