Bytebook Compiler

Logic & Proofs

Propositional Logic

Propositions and the logical connectives — negation, AND, OR, XOR, implication and biconditional — with truth tables, converse and contrapositive, and translating English.

4 min read1 runnable exampleInteractive4-question quiz

Logic is the language of precise reasoning. It underlies every if statement you write, digital circuits, database queries and mathematical proof.

Propositions

A proposition is a declarative statement that is either true (T) or false (F), but not both.

Statement Proposition?
"7 is a prime number." yes — true
"Delhi is the capital of Kerala." yes — false
"x + 2 = 5" no — its truth depends on x
"Close the door." no — a command
"Is it raining?" no — a question

We name propositions with letters p, q, r …

Connectives

Name Symbol Read as True when
Negation ¬p not p p is false
Conjunction p ∧ q p and q both are true
Disjunction p ∨ q p or q at least one is true
Exclusive or p ⊕ q p xor q exactly one is true
Implication p → q if p then q except when p is true and q is false
Biconditional p ↔ q p if and only if q p and q have the same value
p q ¬p p ∧ q p ∨ q p ⊕ q p → q p ↔ q
T T F T T F T T
T F F F T T F F
F T T F T T T F
F F T F F F T T

Implication: the tricky one

"If you score above 90, you get an O grade." The promise is broken only when you score above 90 and do not get an O. If you score below 90, the promise says nothing — so the implication is true (vacuously).

Ways to say p → q in English: "if p, then q", "p implies q", "q if p", "p only if q", "p is sufficient for q", "q is necessary for p", "q whenever p".

From p → q we form three related statements:

Name Form Equivalent to p → q?
Converse q → p no
Inverse ¬p → ¬q no
Contrapositive ¬q → ¬p yes

"If it rains, the ground is wet." Contrapositive: "If the ground is not wet, it did not rain" — same meaning. Converse: "If the ground is wet, it rained" — not the same (a sprinkler could have wet it).

Build any truth table

Type a formula and press Enter. Use ! or ~ for NOT, & for AND, | for OR, ^ for XOR, -> for implies and <-> for if and only if.

Truth tables in code

With n variables a truth table has 2ⁿ rows. Python can generate it:

Python
from itertools import product

def implies(a, b):
    return (not a) or b

print(f"{'p':5} {'q':5} {'r':5} | (p→q)∧(q→r) → (p→r)")
for p, q, r in product([True, False], repeat=3):
    value = implies(implies(p, q) and implies(q, r), implies(p, r))
    print(f"{p!s:5} {q!s:5} {r!s:5} | {value}")
Output
p     q     r     | (p→q)∧(q→r) → (p→r)
True  True  True  | True
True  True  False | True
True  False True  | True
True  False False | True
False True  True  | True
False True  False | True
False False True  | True
False False False | True

Every row is True: this formula (the hypothetical syllogism) is a tautology.

Tautology, contradiction, contingency

Kind Meaning Example
Tautology true in every row p ∨ ¬p
Contradiction false in every row p ∧ ¬p
Contingency true in some rows, false in others p → q

Precedence

¬ binds tightest, then ∧, then ∨, then →, then ↔. So ¬p ∨ q ∧ r means (¬p) ∨ (q ∧ r). Use brackets when in doubt.

Translating English

Let p = "it is raining", q = "I carry an umbrella", r = "I get wet".

English Logic
It is raining and I don't carry an umbrella. p ∧ ¬q
If it rains and I have no umbrella, I get wet. (p ∧ ¬q) → r
I get wet only if it is raining. r → p
I carry an umbrella if and only if it is raining. q ↔ p

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    When is p → q false?

  2. 2

    What is the contrapositive of "If n is even, then n² is even"?

  3. 3

    How many rows does a truth table with 4 variables have?

  4. 4

    Which is a tautology?