TExcelTAD
Home › Discrete Mathematics › Logical Connectives and Truth Tables

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 ¬P\neg P not P PP is false
AND P∧QP \land Q P and Q both are true
OR (inclusive) P∨QP \lor Q P or Q at least one is true
XOR (exclusive or) P⊕QP \oplus Q P != Q, P ^ Q exactly one is true
IMPLIES P→QP \to Q (not P) or Q not (PP true and QQ false)
IFF P↔QP \leftrightarrow Q P == Q both have the same value

Python has no implication operator, so we write it as (not P) or Q, using the equivalence P→Q≡¬P∨QP \to Q \equiv \neg P \lor Q.

python · try it yourself
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 nn variables has 2n2^n rows: every combination of truth values. Evaluate it from the inside out. For (P∨Q)∧¬(P∧Q)(P \lor Q) \land \neg (P \land Q):

PP QQ P∨QP \lor Q P∧QP \land Q ¬(P∧Q)\neg(P \land Q) 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 P⊕QP \oplus Q: this formula is one way to express "exactly one".

A tiny truth-table generator works for any formula written as a Python function:

python · try it yourself
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
"PP unless QQ" ¬Q→P\neg Q \to P (equivalently P∨QP \lor Q)
"PP only if QQ" P→QP \to Q
"PP is necessary for QQ" Q→PQ \to P
"PP is sufficient for QQ" P→QP \to Q
"neither PP nor QQ" ¬P∧¬Q\neg P \land \neg Q

"Only if" trips people up. "You can board only if you have a ticket" means boarding implies having a ticket (B→TB \to T); 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 ∧\land 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:

python · try it yourself
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 ¬P∨(P∧Q)\neg P \lor (P \land Q). Which simpler formula has the same table?

Answer

Values (TT, TF, FT, FF): T, F, T, T. It equals P→QP \to Q.

Solution
PP QQ ¬P\neg P P∧QP \land Q 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 PP true, QQ false: exactly the table of P→QP \to Q.

Exercise 2: Translate

Let RR = "it rains", UU = "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) U→RU \to R; (b) R→UR \to U; (c) R→UR \to U.

Solution

(a) "UU only if RR" is U→RU \to R. (b) "RR is sufficient for UU" is R→UR \to U. (c) "UU unless ¬R\neg R" is ¬¬R→U\neg\neg R \to U, which is R→UR \to U.

Exercise 3: Count the true rows (code)

How many of the 16 rows make (A→B)∧(B→C)∧(C→D)(A \to B) \land (B \to C) \land (C \to D) true?

Answer
Solution

The formula says: once a variable in the chain A,B,C,DA, B, C, D is true, all later ones are true. So the true rows are FFFF, FFFT, FFTT, FTTT, TTTT: 5 rows.

python · try it yourself
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?

  1. 5
  2. 10
  3. 25
  4. 32

Which Python expression computes P→QP \to Q for Booleans P, Q?

  1. P and Q
  2. not P or Q
  3. P or not Q
  4. P == Q

"You may enter only if you are a member" is best written as…

  1. member→enter\text{member} \to \text{enter}
  2. enter→member\text{enter} \to \text{member}
  3. enter∧member\text{enter} \land \text{member}
  4. enter⊕member\text{enter} \oplus \text{member}