Learning objectives
By the end of this week you can:
- 1 Decide whether a sentence is a proposition, and explain why questions, commands and opinions are not.
- 2 Use the connectives ¬, ∧, ∨, ⊕, →, ↔ and write their truth tables from memory.
- 3 Build the truth table of any compound proposition and know that n variables give 2ⁿ rows.
- 4 Explain why p → q is true when p is false, and translate “if”, “only if”, “unless”, “necessary” and “sufficient”.
- 5 Form the converse, inverse and contrapositive, and know which one is equivalent to the original.
- 6 Implement all of the above in Python (Lab 1).
★ the wow moment
A truth table for 300 variables has more rows than there are atoms in the universe.
n variables give 2ⁿ rows, and 2³⁰⁰ is about 10⁹⁰, while the observable universe has roughly 10⁸⁰ atoms. Yet industrial SAT solvers decide formulas with millions of variables every day, to verify chips and schedule airlines. Today you learn the truth table; by week 3 you will see why clever search beats brute force.
Propositions
Two conditions: it must state something (declarative), and it must have a definite truth value, true (T, 1) or false (F, 0). We name propositions with lowercase letters
Answers
- Proposition, and it is false (the capital is Ankara). False propositions are still propositions.
- Proposition, true.
- Not a proposition. It is an opinion; it has no definite truth value.
- Not a proposition. A question states nothing.
- Not a proposition. A command.
- Not a proposition. A number on its own asserts nothing. “2375 is odd” would be a proposition (a true one).
- Proposition, true.
- Not a proposition until we know . It is a propositional function ; is true, is false. Predicates are the topic of week 4.
Negation
| p | ¬p |
|---|---|
| 1 | 0 |
| 0 | 1 |
Negate the whole statement, not a word in it. The negation of “the queue is empty” is “the queue is not empty”, and the negation of is (not ).
Connectives
Connectives build compound propositions from simpler ones. Here are all of them in one table: memorize it, you will use it every week.
| p | q | p ∧ q | p ∨ q | p ⊕ q | p → q | p ↔ q |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 0 | 0 | 0 | 0 | 0 | 1 | 1 |
| Symbol | Name | Read as | True when | Python |
|---|---|---|---|---|
| conjunction | and | both are true | p and q | |
| disjunction | or | at least one is true (inclusive) | p or q | |
| exclusive or | xor | exactly one is true | p != q | |
| conditional | if then | not ( true and false) | (not p) or q | |
| biconditional | if and only if | both have the same value | p == q |
Truth tables
A truth table lists every combination of truth values of the variables and the resulting value of the formula. With variables there are rows, because each variable doubles the number of cases.
Build it column by column, from the inside out. For :
| p | q | ¬q | p ∨ ¬q | p ∧ q | (p ∨ ¬q) → (p ∧ q) |
|---|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 | 0 | 0 |
Now let the computer do it. Type any formula, including the one above as (p | ~q) -> (p & q):
~ not · & and · | or · ^ xor · -> implies · <-> iff · variables p q r s …
python ›
The conditional, carefully
is the connective students get wrong most often, and it is the one programmers rely on most. is the hypothesis (antecedent), the conclusion (consequent).
Think of it as a promise. “If you enter valid credentials, then you will be logged in.”
| Credentials | Result | Promise |
|---|---|---|
| valid | logged in | kept |
| valid | not logged in | broken |
| invalid | logged in | not broken: no promise was made about invalid credentials |
| invalid | not logged in | not broken |
The promise only says something when the hypothesis holds. When is false, is vacuously true. That is why has the same truth table as , and why Python code often writes implications as not p or q.
Many ways to say
| English | Logic |
|---|---|
| if , then · if · whenever | |
| only if | |
| is sufficient for | |
| is necessary for | |
| unless |
Converse, inverse, contrapositive
From we can build three related conditionals:
| Name | Form | Equivalent to ? |
|---|---|---|
| converse | no | |
| inverse | no | |
| contrapositive | yes |
“If today is Saturday, then tomorrow is Sunday.” Converse: “If tomorrow is Sunday, today is Saturday”, which happens to be true here, but only because of how the calendar works, not because of logic. Inverse: “If today is not Saturday, tomorrow is not Sunday.” Contrapositive: “If tomorrow is not Sunday, today is not Saturday”, which is always as true as the original. Check all of them in the playground: type (p -> q) <-> (~q -> ~p) and then (p -> q) <-> (q -> p).
The biconditional
(“ if and only if ”, often written “iff”) is true when and have the same truth value. It means : is both necessary and sufficient for . Notice that is exactly . In code, it is p == q on booleans. We will go deeper next week.
Translating English into logic
- Find the atomic propositions and name them.
- Find the connectives, paying special attention to “only if”, “unless”, “but” (which means ) and “neither … nor” ().
- Add parentheses to match the intended meaning.
Answers
- “ unless ” means “if not then ”: (equivalently ).
Logic on bits
Computers store truth values as bits, 1 and 0, and apply connectives to whole bit strings position by position. Python does this with integers:
a = 0b0110_1100
b = 0b1010_1010
print(f"{a & b:08b}") # AND → 00101000
print(f"{a | b:08b}") # OR → 11101110
print(f"{a ^ b:08b}") # XOR → 11000110
This is how file permissions (chmod 755), network masks and feature flags work. You will use it in the lab.
Summary
- A proposition is a declarative sentence with exactly one truth value.
- Six operators: . Know their tables cold.
- variables → rows.
- is false only when is true and is false; it equals .
- (contrapositive). The converse is not equivalent.
- “ only if ” is ; necessary conditions are conclusions, sufficient conditions are hypotheses.
In industry
-
Every if-statement
Conditions in code are compound propositions. A wrong negation (“not (a and b)” vs “not a and not b”) is one of the most common bugs in code review.
-
Access control
Rules like “admin, or owner and not suspended” are propositions evaluated on every request of a web app.
-
Hardware
AND, OR, NOT and XOR are physical gates. Your CPU is billions of tiny truth tables.
-
Requirements
“The alarm sounds only if the door is open” and “if the door is open the alarm sounds” are different requirements. Engineers must tell them apart.
Lab 1: Truth-Table Engine
Write the connectives in Python, generate every row of a truth table, and build a tool that decides tautologies, contradictions and equivalences.
due Fri, Oct 2 · 23:59 · 10 points · open now
Go deeper
- Stanford CS103 truth table tool · check your hand-made tables
- forall x: Calgary, an open logic textbook · Chapters 1–11: sentences, connectives, truth tables
- Discrete Mathematics: An Open Introduction (Oscar Levin) · Section 0.2, mathematical statements
- Mathematics for Computer Science · Chapter 3.1–3.3, propositional formulas
Tags:propositiontruth tablenegationconjunctiondisjunctionXORimplicationconditionalbiconditionalcontrapositiveconverse