MASTER YOUR LOGIC BUILDING:Phase 1 - Level 4: Logical Operators & Compound Statements

Mastering phase 1 - level 4: logical operators & compound statements concepts and implementation.

Phase 1 - Level 4: Logical Operators & Compound Statements

Why This Matters

Signing up for a website? Your password must be long enough and contain a digit. Getting a discount? You might qualify if you're a student or a senior citizen. Real rules often need more than one condition — and that's exactly what logical operators are for.

You've seen and, or, and not briefly. Now you'll use them confidently in trickier problems.

What You'll Learn

  • How and, or, and not work in detail
  • How to combine multiple conditions in one if statement
  • Which operator runs first (precedence)
  • How to solve classic problems like FizzBuzz and password checks

The Three Operators — A Quick Refresher

OperatorPlain EnglishExample
`and`**Both** must be true`age >= 18 and has_id`
`or`**At least one** must be true`is_weekend or is_holiday`
`not`**Flip** true to false`not is_logged_in`

AND in action

if len(password) >= 8 and has_digit:
    print("Valid password")

Both rules must pass. Fail either one → invalid.

OR in action

if num % 3 == 0 or num % 5 == 0:
    print("Divisible by 3 or 5")

Either condition is enough.

NOT in action

if not has_digit:
    print("- Password must contain at least one digit")

Useful for clear error messages.

Operator Precedence — Who Goes First?

When you mix operators, Python follows this order:

  1. not (highest)
  2. and
  3. or (lowest)
# Without parentheses — might surprise you!
if True or False and False:   # and runs first → True or False → True
    print("Runs!")

# With parentheses — much clearer
if (True or False) and False:  # False
    print("Doesn't run")
Tip: When in doubt, add parentheses. Your future self will thank you.

Step-by-Step: FizzBuzz Logic

For a given number:

  1. First check: divisible by 3 and 5? → print "FizzBuzz"
  2. Then check: divisible by 3 only? → print "Fizz"
  3. Then check: divisible by 5 only? → print "Buzz"
  4. Otherwise → print the number
if num % 3 == 0 and num % 5 == 0:
    print("FizzBuzz")
elif num % 3 == 0:
    print("Fizz")
elif num % 5 == 0:
    print("Buzz")
else:
    print(num)

Order matters! Check the combined condition before the individual ones.

Common Mistakes

  • Checking Fizz and Buzz before FizzBuzz — you'll never reach the combined case
  • Using `or` when you mean `and` — "must be 8+ chars AND have a digit" needs and
  • Skipping parentheses in long conditions — hard to read and easy to get wrong
  • Writing one giant `if` — break conditions into named variables for clarity:
has_length = len(password) >= 8
has_digit = any(char.isdigit() for char in password)
if has_length and has_digit:
    print("Valid")

Tips

  • Name your conditions (has_length, has_digit) — code reads like English
  • Check the most specific condition first (FizzBuzz before Fizz)
  • Test with edge cases: 0, 15 (both 3 and 5), 9 (only 3), 10 (only 5)

Practice Focus

Try the examples below:

  • FizzBuzz — get the order of conditions right
  • Password validation — combine and with helpful not error messages
  • Practice rewriting a long condition as separate named variables

Hands-on Examples

FizzBuzz Problem

# Take a number
num = int(input("Enter a number: "))

# Check divisibility
if num % 3 == 0 and num % 5 == 0:
    print("FizzBuzz")
elif num % 3 == 0:
    print("Fizz")
elif num % 5 == 0:
    print("Buzz")
else:
    print(num)

Check for both conditions first (divisible by 3 AND 5), then individual conditions. Order matters!