Computer Science FundamentalsLesson 2 of 3 · tap to see all lessons
Mathematics · Beginner
Truth Tables and Logical Operators: AND, OR, NOT
Learn the logical operators AND, OR, and NOT, build truth tables step by step, and see how the same logic decides every if statement in code.
- Qyubie Learn Editorial Team
- Published
- 7 min read
Includes 2 exercises and 1 quick check you can do right here.
Every time a program decides something — is the password right AND is the account active? — it uses logic. The rules are simple, and a truth table lets you see all of them at once. This topic appears in discrete mathematics, digital electronics, and every programming course.
By the end of this guide you will be able to:
- tell what a proposition is and give it a truth value,
- use NOT, AND, and OR,
- build a truth table for a compound statement, and
- write the same logic in Python.
What you need: no math beyond counting. Examples run in your browser — press Run Python.
Propositions: statements that are true or false
A proposition is a statement that is either true or false — never both, never "maybe".
- "7 is greater than 3." → a proposition (true)
- "Manila is in Japan." → a proposition (false)
- "Close the door." → not a proposition; it's a command, not true or false.
In logic we name propositions with letters like p and q, and write their values as T (true) and F (false). In code, they're True and False — called Boolean values.
Try it · Python
NOT: flipping a value
NOT p (written ¬p) is true when p is false, and false when p is true.
| p | NOT p |
|---|---|
| T | F |
| F | T |
AND: both must be true
p AND q (written p ∧ q) is true only when both p and q are true.
| p | q | p AND q |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
Everyday example: "You may enter if you have a ticket AND an ID." Missing either one means no entry.
OR: at least one is true
p OR q (written p ∨ q) is true when at least one of them is true. It's only false when both are false.
| p | q | p OR q |
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
Careful: in everyday speech, "tea or coffee?" usually means one, not both. Logical OR is inclusive — both being true still counts as true.
Quick check
Building a truth table for a compound statement
For a statement with several parts, list every combination of inputs, then work out each part in its own column. With 2 inputs there are 4 rows; with 3 inputs, 8 rows (2ⁿ rows for n inputs).
Let's do p AND (NOT q):
| p | q | NOT q | p AND (NOT q) |
|---|---|---|---|
| T | T | F | F |
| T | F | T | T |
| F | T | F | F |
| F | F | T | F |
The helper column "NOT q" makes the last column easy: it's just AND of the p column and the NOT q column.
Python can produce the same table. Its operators are spelled as words — and, or, not:
Try it · Python
Exercise · Python
Truth table for OR
Print the truth table for p or q: one line per row, in the order TT, TF, FT, FF, each in the form True True True (p, q, then the result).
Expected output
True True True True False True False True True False False False
Hint
Replace pass with print(p, q, p or q) — print puts a single space between values.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
for p in [True, False]:
for q in [True, False]:
print(p, q, p or q)
Order of operations
Just like × comes before + in arithmetic, logic has an order: NOT first, then AND, then OR. So p or q and r means p or (q and r).
Try it · Python
When in doubt, use parentheses. They cost nothing and make your meaning clear to the reader as well as the computer.
Logic in real code
Every if statement evaluates a logical expression:
Try it · Python
Exercise · Python
Can this student enroll?
A student can enroll if they passed the entrance exam AND (they paid the fee OR they have a scholarship). With the values given, print True or False using one logical expression.
Expected output
True
Hint
Translate the sentence directly: passed_exam and (paid_fee or has_scholarship). The parentheses matter.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
passed_exam = True paid_fee = False has_scholarship = True can_enroll = passed_exam and (paid_fee or has_scholarship) print(can_enroll)
Common mistakes
- Treating OR as "one but not both". Logical OR is true when both sides are true. (The "one but not both" operator exists — it's called XOR.)
- Forgetting rows. n inputs need 2ⁿ rows. A 3-input table with 6 rows is missing two.
- Ignoring precedence. NOT, then AND, then OR — or add parentheses.
Summary
- A proposition is a statement that is true or false.
- NOT flips a value; AND needs both sides true; OR needs at least one.
- A truth table lists every input combination (2ⁿ rows) with a column for each step.
- Programs use exactly this logic in their
ifconditions.
Next steps: build the truth table for NOT (p AND q) and for (NOT p) OR (NOT q) by hand. Compare the last columns — you'll discover one of De Morgan's laws.