Set Operations

Carson West

Frozen Sets

Set Operations

Set operations in Python leverage the power of the set data structure for efficient manipulation of unique elements. Key operations include:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2  # or set1.union(set2)
print(union_set)  # Output: {1, 2, 3, 4, 5}
intersection_set = set1 & set2  # or set1.intersection(set2)
print(intersection_set)  # Output: {3}
difference_set = set1 - set2  # or set1.difference(set2)
print(difference_set)  # Output: {1, 2}
symmetric_difference_set = set1 ^ set2  # or set1.symmetric_difference(set2)
print(symmetric_difference_set)  # Output: {1, 2, 4, 5}
set3 = {1,2}
is_subset = set3.issubset(set1) # or set3 <= set1
is_superset = set1.issuperset(set3) # or set1 >= set3
print(is_subset) #Output: True
print(is_superset) #Output: True

Set Theory Basics (Python Sets)

Other relevant notes: