#separator:tab #html:false #notetype:Basic #deck:Math::Sets::Reading a set expression #tags:sets math #columns:Front Back Tags #tags column:3 In an unbracketed set expression, which operation is done first, and what is the full order? Complement first, then intersection, then union; equals go left to right. So A ∪ B ∩ C means A ∪ (B ∩ C). Difference has no agreed place: Python reads it above ∩, Isabelle, Pascal and SQL level with ∪, and most textbooks bracket it always. precedence Why is the order of set operations complement, intersection, union? It copies logic, where not binds tighter than and, and and tighter than or: ′ is not, ∩ is and, ∪ is or. Difference is 'and not', two connectives, so logic gives it no place of its own. precedence How does Python group A | B & C? A | (B & C): & binds tighter than |, the same as ∩ before ∪. python List Python's set operators from tightest to loosest. - then & then ^ then |. Inherited from C's bitwise operators; it matches the shared textbook order for ∩ and ∪, and puts - (difference) above & by Python's own choice. python How is A ∖ B ∖ C read, and does it matter? (A ∖ B) ∖ C, left to right. It matters: A ∖ (B ∖ C) is a different set, because difference is not associative. precedence With U = {1, 2, 3, 4, 5}, evaluate {5} ∪ {1, 2}′ ∩ {2, 3} by the precedence rule, and with the other grouping. By the rule ∩ goes first: {5} ∪ ({3, 4, 5} ∩ {2, 3}) = {5} ∪ {3} = {3, 5}. Grouped as ({5} ∪ {3, 4, 5}) ∩ {2, 3} it is {3}. Two answers from one line of symbols, so bracket when in doubt. precedence What is wrong with writing x ∈ A ∩ x ∈ B? ∩ needs a set on each side, but 'x ∈ A' is a statement, true or false. Write x ∈ A ∧ x ∈ B, or x ∈ A ∩ B. trap What is wrong with writing x ∈ A ∧ B? ∧ needs a statement on each side, but B is a set. Write x ∈ A ∧ x ∈ B. trap What does Python do with x in A & x in B? & binds tighter than in, so it reads x in (A & x) in B, a chained comparison that tries to compute A & 2 and raises TypeError. python What does x in A and B return in Python, and why is it dangerous? If x is in A it returns the set B itself, which is truthy even when x is not in B, so an if takes the wrong branch and nothing complains. Write x in A and x in B. python Using Kuratowski pairs, (a, b) = {{a}, {a, b}}, what is (1, 2) ∩ (2, 1)? {{1, 2}}. (1, 2) = {{1}, {1, 2}} and (2, 1) = {{2}, {1, 2}} share only the member {1, 2}. notation Read as open intervals, what is (1, 2) ∪ (2, 1)? (1, 2). The interval (2, 1) is the set of t with 2 < t < 1, which is empty. Same symbols as the ordered pair, a different set: the context decides. notation Where do real systems put set difference ∖ in the order of operations? Python: above ∩ (- before &). Lean 4: level with ∩ at 70, above ∪ at 65, and non-associative. Isabelle/HOL, Pascal, the SQL standard, Z: level with ∪, below ∩. Most textbooks: no level, always bracketed. precedence Which unbracketed expressions change value with the convention for ∖, and which does not? A ∪ B ∖ C and A ∖ B ∩ C change: Python reads A ∪ (B ∖ C) and (A ∖ B) ∩ C, Isabelle (A ∪ B) ∖ C and A ∖ (B ∩ C). A ∩ B ∖ C does not, because A ∩ (B ∖ C) = (A ∩ B) ∖ C is a law. precedence Why does A ∖ B ∪ C ∩ D′ not test which convention for ∖ a reader uses? Its ∖ is leftmost and its ∪ is lowest in every system, so all read it as (A ∖ B) ∪ (C ∩ D′). To tell conventions apart, put ∖ beside a ∪ or ∩ that could take it either way: A ∪ C ∖ B. precedence