Set Operations
Sets support mathematical set operations: union, intersection, difference, and symmetric difference — both as methods and as operators.
Union (| or union())
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a | b) # {1, 2, 3, 4, 5, 6}
print(a.union(b)) # same resultIntersection (& or intersection())
print(a & b) # {3, 4}
print(a.intersection(b))Difference (- or difference())
print(a - b) # {1, 2} (in a, not in b)
print(b - a) # {5, 6} (in b, not in a)Symmetric Difference (^ or symmetric_difference())
print(a ^ b) # {1, 2, 5, 6}
print(a.symmetric_difference(b))Subset & Superset
c = {1, 2}
print(c.issubset(a)) # True (c ⊆ a)
print(a.issuperset(c)) # True (a ⊇ c)
print(a.isdisjoint({7, 8})) # True (no common elements)Update In-Place
a |= {10, 11} # union update
a &= b # intersection update
a -= b # difference update