Lesson 7: Set operations
With two sets you can ask comparison questions: who's in both (intersection &), who's in at least one (union |), and who's in the first but not the second (difference -). Each operation returns a new set. Since a set has no order, we'll use sorted() to show a clear result.
Two sets are two circles. The intersection is the overlap in the middle, the union is both circles together, and the difference is what's in one circle but not the other.
- union
- All values that are in at least one of the sets, written a | b.
- intersection
- The values that are in both sets, written a & b.
- difference
- The values in the first set but not in the second, written a - b.