Commonly sets interact. For example, you and a new roommate decide to have a house party, and you both invite your circle of friends. At this party, two sets are being combined, though it might turn out that there are some friends that were in both sets.
Notice that in the example above, it would be hard to just ask for since everything from the color fuchsia to puppies and peanut butter are included in the complement of the set. For this reason, complements are usually only used with intersections, or when we have a universal set in place.
As we saw earlier with the expression set operations can be grouped together. Grouping symbols can be used like they are with arithmetic - to force an order of operations.
# The book's animal sets (it writes F as P in a couple of spots - same set).
H = {"cat", "dog", "rabbit", "mouse"}
F = {"dog", "cow", "duck", "pig", "rabbit"}
W = {"duck", "rabbit", "deer", "frog", "mouse"}
U = H | F | W
def show(label, S):
print(f"{label:<22} = {{{', '.join(sorted(S))}}}")
show("H", H)
show("F", F)
show("W", W)
print()
show("H inter F", H & F)
print("...then union with W:")
show("(H inter F) union W", (H & F) | W)
print()
show("F union W", F | W)
print("...then intersect with H:")
show("H inter (F union W)", H & (F | W))
print()
show("(H inter F)c inter W", (U - (H & F)) & W)
print("\nDoes the grouping matter?")
left = (H & F) | W
right = H & (F | W)
print(f" (H inter F) union W has {len(left)} elements")
print(f" H inter (F union W) has {len(right)} elements")
print(f" same set? {left == right}")
print(f" difference: {sorted(left ^ right)}")
# Try it: move "duck" from F into H and re-run. Which of the three answers change?
Adapted from Math in Society by David Lippman, hosted on LibreTexts (math.libretexts.org) and licensed under CC BY-SA 3.0. Changes were made. License: CC-BY-SA-3.0.
These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.