TL;DR:
Boolean values are true or false. AND is true only if both inputs are true, OR is true if at least one is, and NOT flips a value.
A truth table lists every combination of inputs and the output for each. With n inputs there are
2ⁿrows.De Morgan's laws: NOT (A AND B) equals (NOT A) OR (NOT B), and NOT (A OR B) equals (NOT A) AND (NOT B).
A home alarm should sound only if it is switched on and either the door opens or motion is detected. Say that sentence in code and you have used Boolean logic. It is the same logic inside every condition you write in a program and every circuit in a processor.
The idea in plain words
A Boolean value is either true or false (1 or 0). Three operations combine them.
AND is true only when both inputs are true.
OR is true when at least one input is true.
NOT flips true to false and false to true.
A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 0 |
1 | 1 | 1 | 1 | 0 |
Why truth tables
A truth table removes guessing. You list every possible combination of inputs and work out the output for each. With one input there are 2 rows, with two there are 4, with three there are 2³ = 8. Checking all rows is the only way to be sure a condition does what you meant.
Worked example: the door alarm
Three inputs: A is armed, D is door open, M is motion detected. The alarm sounds when the system is armed and either the door is open or motion is detected.
alarm = A AND (D OR M)Work out D OR M first, then AND it with A. The parentheses matter: without them, AND is evaluated before OR, so A AND D OR M would sound the alarm even when the system is off and there is motion.
A | D | M | D OR M | Alarm |
|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 1 | 1 |
1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 1 |
Read the table. With the system off (A = 0), the alarm never sounds. With it armed, the alarm sounds in the three rows where the door is open or there is motion, and stays quiet only in the row where neither happens. The alarm is on in 3 of 8 rows.
In Python this is a direct translation.
def alarm(armed, door_open, motion):
return armed and (door_open or motion)
print(alarm(True, False, True))
print(alarm(False, True, True))
print(alarm(True, False, False))The output is True, False, False. These match rows 6, 4 and 5 of the table.
De Morgan's laws
Sometimes you need the opposite condition: when should the alarm stay silent? Negating the whole expression gives NOT (A AND (D OR M)). De Morgan's laws let you push the NOT inward.
NOT (X AND Y) = (NOT X) OR (NOT Y)NOT (X OR Y) = (NOT X) AND (NOT Y)
Apply the first law with X = A and Y = (D OR M): silent is (NOT A) OR NOT (D OR M). Apply the second law to the second part: NOT (D OR M) = (NOT D) AND (NOT M). So the alarm is silent when (NOT A) OR ((NOT D) AND (NOT M)). In words: the system is off, or the door is closed and there is no motion.
Check against the table. Row 6 (A = 1, D = 0, M = 1): NOT A is 0; NOT D is 1 but NOT M is 0, so the AND is 0; silent is 0. The alarm is on, as the table says. Row 5 (A = 1, D = 0, M = 0): NOT D and NOT M are both 1, so silent is 1, and the table shows no alarm. It works.
Proving De Morgan with a small table
You do not have to take the laws on trust. For two inputs there are only four rows, so you can check the first law directly.
A | B | NOT (A AND B) | (NOT A) OR (NOT B) |
|---|---|---|---|
0 | 0 | 1 | 1 |
0 | 1 | 1 | 1 |
1 | 0 | 1 | 1 |
1 | 1 | 0 | 0 |
The same check works for the second law. Whenever two expressions give the same output on every row of the table, they are equivalent, and you can swap one for the other. Programmers use this to simplify conditions: not (a and b) can be rewritten as (not a) or (not b), whichever reads more clearly.
How to check your answer
Count the rows:
2ⁿfor n inputs.Fill the table in a fixed order, such as counting in binary from 000 to 111, so no row is missed or repeated.
Check two rows by thinking in plain words.
Compare your expression with its negation: they should disagree in every row.
Common mistakes
Mixing up AND and OR when translating English. 'Either door or motion' is OR.
Forgetting that NOT applies to a whole bracket, not just the first term.
Applying NOT inside a bracket but forgetting to switch AND and OR.
Missing parentheses, so AND binds more tightly than you meant.
Where you will use it next
Boolean logic drives if-statements, search filters and database WHERE clauses. It is also how the hardware does arithmetic, with bits as in binary numbers and why computers use them. Lookups with true-or-false answers appear in hash tables and why lookups are fast.
Frequently asked questions
What is XOR?
Exclusive OR is true when exactly one input is true. It is an extra operation built from the three basics.
Is `and` in Python always evaluated left to right?
Yes, and it stops early: if the left side is false, the right side is not evaluated.
