Python Basics
Python Compound Conditions
1. What is a Compound Condition?
- A condition that combines multiple conditions together
- Uses logical operators to chain conditions
- Still evaluates to a single
TrueorFalse
2. The 3 Logical Operators
and
- Returns
Trueonly if both sides areTrue - If either side is
False→ result isFalse
| Left | Right | Result |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
x, y = 2, 4
x < y and y + 2 > 3 # True and True → True
x < y and False # True and False → False
or
- Returns
Trueif at least one side isTrue - Returns
Falseonly if both sides areFalse
| Left | Right | Result |
|---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
x < y or False # True or False → True
False or False # False
not
- Negates / reverses a condition
not True→Falsenot False→True
not (x < y) # not True → False
not (x < y and 1 == 1) # not True → False
⚠️ Precedence warning:
nothas lower precedence than comparison operators (==,<, etc.) Use parentheses when in doubt
True == not False # ❌ SyntaxError
True == (not False) # ✅ True
3. Logical Operators in Other Languages (Reference)
| Python | Other Languages | Meaning |
|---|---|---|
not |
! |
NOT |
and |
&& |
AND |
or |
\|\| |
OR |
4. Operator Precedence (Highest → Lowest)
()Parentheses- Comparison / conditional operators (
==,!=,<,>,<=,>=) notandor
Worked Example
True or False and not True or False
Step 1 — not: not True → False
True or False and False or False
Step 2 — and: False and False → False
True or False or False
Step 3 — or (left to right): True or False → True → True or False → True
5. De Morgan's Law
A rule from Boolean algebra for simplifying compound conditions.
The Two Laws
not (X and Y) == (not X) or (not Y)
not (X or Y) == (not X) and (not Y)
How to Apply It
- Move
notinside the parentheses - Swap
and↔or - Apply
notto each operand individually
Double not Cancels Out
not not False # → False (two nots cancel)
not not not X # → not X (two cancel, one remains)
Example
not (W and Z or not W)
Step 1 — Fix precedence with parentheses (and before or):
not ((W and Z) or (not W))
Step 2 — Apply De Morgan's (swap or → and, apply not to each side):
not(W and Z) and not(not W)
Step 3 — Apply De Morgan's again to left side:
(not W or not Z) and W
Step 4 — Double not cancels:
(not W or not Z) and W ✅ simplified
6. Truth Tables
A truth table lists every possible combination of variable values to determine when a condition is True or False.
How to Build One
- One column per variable
- Number of rows = 2ⁿ (n = number of variables)
- Fill columns alternating: every 1, every 2, every 4 ...
Example — 2 variables (W, Z) → 4 rows
| W | Z | (not W or not Z) and W |
|---|---|---|
False |
False |
True |
True |
False |
True |
False |
True |
True |
True |
True |
False |
Truth tables are useful for finding which values make a condition
TrueorFalse
7. Key Takeaways & Recap
and→ both sides must beTrue;or→ at least one side must beTrue;not→ reverses- Precedence:
()→ comparisons →not→and→or - Wrap
nottargets in parentheses to avoid unexpected behavior - De Morgan's Law:
not(X and Y)=not X or not Y;not(X or Y)=not X and not Y - Two
nots cancel each other out - Truth tables: rows = 2ⁿ combinations; useful for testing all possible inputs