Skip to content
AI-grafen
DAI developerMathematics· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

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}.

OperationSymbolMeansExample
UnionA∪BA \cup Beverything that is in at least one{1,2}∪{2,3}={1,2,3}\{1,2\} \cup \{2,3\} = \{1,2,3\}
IntersectionA∩BA \cap Bwhat is in both{1,2}∩{2,3}={2}\{1,2\} \cap \{2,3\} = \{2\}
DifferenceA∖BA \setminus Bin A but not in B{1,2}∖{2,3}={1}\{1,2\} \setminus \{2,3\} = \{1\}
Belongs tox∈Ax \in Ax is in A2∈{1,2}2 \in \{1,2\}
SubsetA⊆BA \subseteq Beverything in A is in B{1}⊆{1,2}\{1\} \subseteq \{1,2\}

Logic is the same thing but for statements:

SymbolTrue when
AND∧\landboth are true
OR∨\lorat least one is true (not «either or»)
NOT¬\negthe statement is false
IMPLIES⇒\Rightarrowif 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. P⇒QP \Rightarrow Q is false only when PP is true and QQ is false:

PPQQP⇒QP \Rightarrow Q
TTT
TFF
FTT
FFT

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:

¬(P∧Q)≡¬P∨¬Q,¬(P∨Q)≡¬P∧¬Q\neg(P \land Q) \equiv \neg P \lor \neg Q, \qquad \neg(P \lor Q) \equiv \neg P \land \neg Q

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:

SymbolRead asMeans
∀\forall«for all»holds for every element
∃\exists«there exists»at least one element satisfies it

The definition of a subset then becomes: A⊆B  ⟺  ∀x (x∈A⇒x∈B)A \subseteq B \iff \forall x\,(x \in A \Rightarrow x \in B).

Negating a quantifier flips it: ¬∀x P(x)≡∃x ¬P(x)\neg \forall x\, P(x) \equiv \exists x\, \neg P(x). 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

All the sources and licences