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 True or False

2. The 3 Logical Operators

and

  • Returns True only if both sides are True
  • If either side is False → result is False
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 True if at least one side is True
  • Returns False only if both sides are False
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 → False
  • not False → True
not (x < y)         # not True  → False
not (x < y and 1 == 1)   # not True → False

⚠️ Precedence warning: not has 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)

  1. () Parentheses
  2. Comparison / conditional operators (==, !=, <, >, <=, >=)
  3. not
  4. and
  5. or

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

  1. Move not inside the parentheses
  2. Swap and ↔ or
  3. Apply not to 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 True or False


7. Key Takeaways & Recap

  1. and → both sides must be True; or → at least one side must be True; not → reverses
  2. Precedence: () → comparisons → not → and → or
  3. Wrap not targets in parentheses to avoid unexpected behavior
  4. De Morgan's Law: not(X and Y) = not X or not Y; not(X or Y) = not X and not Y
  5. Two nots cancel each other out
  6. Truth tables: rows = 2ⁿ combinations; useful for testing all possible inputs