Sets and logic
Be able to use set operations and logical connectives and read formal definitions.
Prerequisites
Intuition
A set is a collection of things without an order and without duplicates. {1, 2, 3} is the same set as {3, 1, 2}.
| Operation | Symbol | Means | Example |
|---|---|---|---|
| Union | everything that is in at least one | ||
| Intersection | what is in both | ||
| Difference | in A but not in B | ||
| Belongs to | x is in A | ||
| Subset | everything in A is in B |
Logic is the same thing but for statements:
| Symbol | True when | |
|---|---|---|
| AND | both are true | |
| OR | at least one is true (not «either or») | |
| NOT | the statement is false | |
| IMPLIES | if the first is true the second must be too |
The most common misunderstanding: mathematical OR is inclusive. «Coffee or tea» in mathematics allows both.
Formal
Implication is the hardest one. is false only when is true and is false:
| T | T | T |
| T | F | F |
| F | T | T |
| F | F | T |
The last two rows surprise people: «if the moon is made of cheese then 2+2=5» is true, because the premise is false. A promise that is never triggered is not broken.
De Morgan's laws — used constantly in both logic and code:
In Python: not (a and b) is the same thing as (not a) or (not b). Negating a compound condition flips the connective too — that is the most common bug when simplifying an if statement.
Quantifiers make it possible to write definitions:
| Symbol | Read as | Means |
|---|---|---|
| «for all» | holds for every element | |
| «there exists» | at least one element satisfies it |
The definition of a subset then becomes: .
Negating a quantifier flips it: . The opposite of «all swans are white» is not «no swans are white» but «there is a swan that is not white». That is exactly why a single counterexample is enough to falsify a claim — and why testing works.
Code
A = {1, 2, 3, 4}
B = {3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5} union
print(A & B) # {3, 4} intersection
print(A - B) # {1, 2} difference
print(A ^ B) # {1, 2, 5} symmetric difference (in exactly one)
print(3 in A, {1, 2} <= A) # True True (belongs to, subset)
# De Morgan in practice
for a in (True, False):
for b in (True, False):
assert (not (a and b)) == ((not a) or (not b))
assert (not (a or b)) == ((not a) and (not b))
print("De Morgan holds")
# Quantifiers: all() is ∀, any() is ∃
numbers = [2, 4, 6, 8]
print(all(x % 2 == 0 for x in numbers)) # True ∀x: x is even
print(any(x > 7 for x in numbers)) # True ∃x: x > 7
# Negation flips the quantifier — the counterexample
print(not all(x > 3 for x in numbers), next(x for x in numbers if not x > 3)) # True 2
# Sets are used for real all the time in data work
train = {"a1", "a2", "a3", "a4"}
test = {"a4", "a5"}
leakage = train & test
print(f"leakage: {leakage or 'none'}") # leakage: {'a4'}
The last example is not made up: checking train & test is the fastest check against data leakage there is, and it takes one line.
Mastery means
- Uses union, intersection, difference and complement
- Uses AND, OR, NOT and implication correctly
- Reads definitions with ∀ and ∃
Sign in to do the exercises and build your mastery up.
Sources
- Matteboken (Mattecentrum) — free to read, non-profit association
- Khan Academy — matematik — CC BY-NC-SA 3.0
- The Python documentation (PSF licence) — PSF