ENع
←Propositional logic
Lesson 06 · Logic and sets

Propositional logic

12 min10 steps5 questionsBeginner
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 6 · Interactive
01 / 10

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'>
Loading figure…
or ← →
Read the lesson step by step
  1. 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'>
  2. 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)
  3. 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
    True
  4. AND 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
    True
  5. OR 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
    False
  6. One 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
    8
  7. Filling 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]
  8. 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]
  9. 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]
  10. 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

Quiz Unlocks after the last step.