Propositional logic
About this lesson
Programs make decisions with true and false. Propositional logic is the maths behind that. Take two statements, p: it is raining, and q: I take an umbrella. Combine them with not, and, or and if-then, build a truth table that covers every case, and see why p → q means the same as (not p) or q.
What a proposition is
A proposition is a statement that is either true or false, never both. Let p be "it is raining" and q be "I take an umbrella". A question such as "Is it raining?" is not a proposition: it has no truth value.
>>> 2 + 2 == 4 # a true proposition True >>> 2 + 2 == 5 # a false one False >>> type(2 + 2 == 5) <class 'bool'>
Read the lesson step by step
What a proposition is
A proposition is a statement that is either true or false, never both. Let p be "it is raining" and q be "I take an umbrella". A question such as "Is it raining?" is not a proposition: it has no truth value.
>>> 2 + 2 == 4 # a true proposition True >>> 2 + 2 == 5 # a false one False >>> type(2 + 2 == 5) <class 'bool'>True or false
Each proposition gets a truth value: T for true or F for false. Python spells them True and False. Say it is raining but I forgot my umbrella: p is T and q is F. A lit lamp (the binary digit 1) means T.
>>> p, q = True, False >>> p, q (True, False) >>> int(p), int(q) # T and F as the bits 1 and 0 (1, 0)NOT flips a value
Connectives build new propositions from old ones. The simplest is negation, written ¬p and read "not p". It flips the value: p is T, so ¬p is F. In Python you write not p.
>>> p = True >>> not p False >>> not not p # two flips cancel out TrueAND needs both
Conjunction, written p ∧ q and read "p and q", is true only when both parts are true. Here p is T but q is F, so p ∧ q is F. In Python you write p and q.
>>> p, q = True, False >>> p and q False >>> True and True TrueOR needs at least one
Disjunction, written p ∨ q and read "p or q", is true when at least one part is true, and also when both are. p is T, so p ∨ q is T. This inclusive or is Python's or.
>>> p, q = True, False >>> p or q True >>> False or False FalseOne row per combination
A truth table lists every combination of values. Each proposition is T or F, so two propositions give 2 × 2 = 4 rows, and n propositions give 2ⁿ rows. Fill in p and q: TT, TF, FT, FF.
>>> from itertools import product >>> list(product([True, False], repeat=2)) [(True, True), (True, False), (False, True), (False, False)] >>> 2 ** 3 # rows for 3 propositions 8Filling AND and OR
Now work out each connective row by row. p ∧ q is T only in row 1, where both are T. p ∨ q is F only in row 4, where both are F. These two columns define AND and OR completely.
>>> rows = [(True, True), (True, False), (False, True), (False, False)] >>> [p and q for p, q in rows] [True, False, False, False] >>> [p or q for p, q in rows] [True, True, True, False]If p, then q
Implication, p → q, reads "if p then q". Treat it as a promise: if it rains, I take an umbrella. The promise is broken only when p is T and q is F, so row 2 is its only F.
>>> def implies(p, q): ... return not (p and not q) # broken only by T, F ... >>> rows = [(True, True), (True, False), (False, True), (False, False)] >>> [implies(p, q) for p, q in rows] [True, False, True, True]Vacuously true
When it is not raining, the promise is never tested, so p → q counts as true whatever q is. That is why rows 3 and 4 are T. Mathematicians call this vacuously true.
>>> p = False # it is not raining >>> [not (p and not q) for q in (True, False)] [True, True]Same column, same meaning
Python has no → operator, so write (not p) or q. Its column, T F T T, matches p → q in every row. Formulas with identical columns are logically equivalent: p → q ≡ ¬p ∨ q.
>>> rows = [(True, True), (True, False), (False, True), (False, False)] >>> [(not p) or q for p, q in rows] [True, False, True, True] >>> all(((not p) or q) == (not (p and not q)) for p, q in rows) True
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which of these is a proposition?
A proposition is a statement that is true or false. "7 is a prime number" is true. A command, a question and an expression with no claim have no truth value.
02p is True and q is False. What does Python print for p and not q?
not binds first: not q is True. Then p and True is True, because both parts are true.
03How many rows does a truth table with 3 propositions, p, q and r, need?
Each proposition is T or F, so there are 2 × 2 × 2 = 2³ = 8 combinations, one row each.
04"If it rains, I take an umbrella." Which situation breaks this promise?
p → q is false only when p is true and q is false. When it does not rain, the promise is never tested, so p → q is true (vacuously true).
05Which formula is logically equivalent to p → q?
¬p ∨ q has the column T F T T, the same as p → q in every row. q → p (T T F T) differs in rows 2 and 3, p ∧ q differs in rows 3 and 4, and ¬p ∧ q (F F T F) differs in rows 1 and 4.
Glossary
- Proposition
- A statement that is either true or false, but not both, such as "it is raining" or "2 + 2 = 5".
- Truth value
- Whether a proposition is true (T) or false (F). Python writes them as True and False.
- Connective
- An operation that builds a new proposition from others: not (¬), and (∧), or (∨), if-then (→).
- Negation (¬p)
- "Not p": true when p is false, and false when p is true.
- Conjunction (p ∧ q)
- "p and q": true only when both p and q are true.
- Disjunction (p ∨ q)
- "p or q": true when at least one of p and q is true (the inclusive or).
- Implication (p → q)
- "If p then q": false only when p is true and q is false; true in every other case.
- Truth table
- A table with one row for every combination of truth values and one column per formula. Two formulas with identical columns are logically equivalent.
Notes and sources
- Python's and and or return one of their operands, not always a bool: 0 or 'hi' gives 'hi'. With True and False as inputs the result is always True or False.
- Python stops early. In p and q, if p is false, q is never evaluated; in p or q, if p is true, q is skipped. This is called short-circuit evaluation.
- not binds tighter than and, and and binds tighter than or, so not p or q means (not p) or q. Brackets make the meaning clear to the reader anyway.
- p → q is not the same as its converse q → p. Row 3 shows the difference: p is F and q is T, so p → q is T but q → p is F.
- De Morgan's laws: ¬(p ∧ q) ≡ ¬p ∨ ¬q and ¬(p ∨ q) ≡ ¬p ∧ ¬q. A four-row truth table checks each one.
- Rosen, Discrete Mathematics and Its Applications, 8th ed., sections 1.1 Propositional Logic and 1.3 Propositional Equivalences
- Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT 6.042J), chapter 3 Logical Formulas
- MIT OpenCourseWare 6.042J Mathematics for Computer Science
- forall x: Calgary, an open introduction to formal logic (truth tables)
- Stanford Introduction to Logic
- Python reference: Boolean operations (not, and, or)
- Python library: Boolean operations and short-circuit evaluation
Quiz Unlocks after the last step.