Set operations compare membership across groups. The result is a new set, leaving the operands unchanged.
left only both right only
cedar oak pine
left = {"cedar", "oak"}
right = {"oak", "pine"}
print(sorted(left - right))
print(sorted(left & right))
print(sorted(left | right))
print(sorted(left ^ right))
['cedar']
['oak']
['cedar', 'oak', 'pine']
['cedar', 'pine']
Difference (-) retains members in the left set but not the right; order matters, so right - left instead contains pine. Intersection (&) keeps common members. Union (|) includes members from either set. Symmetric difference (^) keeps members in exactly one set. sorted returns a list, used here only to make display order predictable.
Converting input lists to sets is useful when finding distinct missing identifiers:
requested = ["A2", "A1", "A2", "A3"]
received = ["A3", "A1"]
missing = set(requested) - set(received)
print(sorted(missing))
print(set(received) <= set(requested))
['A2']
True
<= tests whether every member of the left set is also in the right set, including the case where the sets are equal. Converting to a set loses repetition counts and input order; keep a list or a frequency dictionary when those details matter.