Logical Connectives and Truth Tables
Everyday words like "and", "or" and "if" are ambiguous. "You can have cake or ice cream" usually means not both; "if it rains, the match is cancelled" says nothing about dry days. Mathematics and programming need exact meanings. Propositional logic builds formulas from variables that are true (T) or false (F) using connectives, each defined by a truth table.
The connectives
| Name | Math | Python | True when |
|---|---|---|---|
| NOT | not P |
is false | |
| AND | P and Q |
both are true | |
| OR (inclusive) | P or Q |
at least one is true | |
| XOR (exclusive or) | P != Q, P ^ Q |
exactly one is true | |
| IMPLIES | (not P) or Q |
not ( true and false) | |
| IFF | P == Q |
both have the same value |
Python has no implication operator, so we write it as (not P) or Q, using the equivalence .
from itertools import product
ops = {
"P and Q": lambda p, q: p and q,
"P or Q": lambda p, q: p or q,
"P xor Q": lambda p, q: p != q,
"P -> Q": lambda p, q: (not p) or q,
"P <-> Q": lambda p, q: p == q,
}
T = lambda b: "T" if b else "F"
print("P Q | " + " | ".join(ops))
for p, q in product([True, False], repeat=2):
print(T(p), T(q), "| " + " | ".join(f"{T(f(p, q)):^{len(name)}}" for name, f in ops.items()))P Q | P and Q | P or Q | P xor Q | P -> Q | P <-> Q
T T | T | T | F | T | T
T F | F | T | T | F | F
F T | F | T | T | T | F
F F | F | F | F | T | T
Building a truth table for a formula
A formula with variables has rows: every combination of truth values. Evaluate it from the inside out. For :
| result | |||||
|---|---|---|---|---|---|
| T | T | T | T | F | F |
| T | F | T | F | T | T |
| F | T | T | F | T | T |
| F | F | F | F | T | F |
The result column equals : this formula is one way to express "exactly one".
A tiny truth-table generator works for any formula written as a Python function:
from itertools import product
import inspect
def truth_table(f):
names = list(inspect.signature(f).parameters)
print(" ".join(names), "| out")
for vals in product([True, False], repeat=len(names)):
print(" ".join("T" if v else "F" for v in vals), "|", "T" if f(*vals) else "F")
# "the alarm sounds if the door is open and the system is armed, unless it is a test"
truth_table(lambda door, armed, test: door and armed and not test)door armed test | out
T T T | F
T T F | T
T F T | F
T F F | F
F T T | F
F T F | F
F F T | F
F F F | F
Translating English into logic
| English | Formula |
|---|---|
| " unless " | (equivalently ) |
| " only if " | |
| " is necessary for " | |
| " is sufficient for " | |
| "neither nor " |
"Only if" trips people up. "You can board only if you have a ticket" means boarding implies having a ticket (); having a ticket does not guarantee boarding.
In software
Short-circuit evaluation: Python evaluates a and b from left to right and skips b when a is false, so if i < len(xs) and xs[i] > 0: never indexes out of range. Logically is symmetric, but in code the order of operands matters for side effects and errors. Bitwise operators &, |, ^, ~ apply the same connectives to every bit of an integer at once, which is how flags and permissions are stored:
READ, WRITE, EXEC = 0b100, 0b010, 0b001
perm = READ | WRITE # OR sets bits
print("perm =", bin(perm))
print("can write?", bool(perm & WRITE)) # AND tests a bit
perm ^= WRITE # XOR toggles a bit
print("after toggling WRITE:", bin(perm), " can write?", bool(perm & WRITE))perm = 0b110
can write? True
after toggling WRITE: 0b100 can write? False
Exercises
Exercise 1: Truth table by hand
Make the truth table of . Which simpler formula has the same table?
Answer
Values (TT, TF, FT, FF): T, F, T, T. It equals .
Solution
| result | ||||
|---|---|---|---|---|
| T | T | F | T | T |
| T | F | F | F | F |
| F | T | T | F | T |
| F | F | T | F | T |
The only false row is true, false: exactly the table of .
Exercise 2: Translate
Let = "it rains", = "I take an umbrella". Translate: (a) "I take an umbrella only if it rains"; (b) "Rain is sufficient for me to take an umbrella"; (c) "I take an umbrella unless it doesn't rain".
Answer
(a) ; (b) ; (c) .
Solution
(a) " only if " is . (b) " is sufficient for " is . (c) " unless " is , which is .
Exercise 3: Count the true rows (code)
How many of the 16 rows make true?
Answer
Solution
The formula says: once a variable in the chain is true, all later ones are true. So the true rows are FFFF, FFFT, FFTT, FTTT, TTTT: 5 rows.
from itertools import product
imp = lambda x, y: (not x) or y
rows = [v for v in product([False, True], repeat=4)
if imp(v[0], v[1]) and imp(v[1], v[2]) and imp(v[2], v[3])]
print(len(rows))
print(["".join("T" if x else "F" for x in r) for r in rows])5
['FFFF', 'FFFT', 'FFTT', 'FTTT', 'TTTT']
Test yourself
How many rows does the truth table of a formula with 5 variables have?
- 5
- 10
- 25
- 32
Which Python expression computes for Booleans P, Q?
P and Qnot P or QP or not QP == Q
"You may enter only if you are a member" is best written as…