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, andnotwork in detail - How to combine multiple conditions in one
ifstatement - Which operator runs first (precedence)
- How to solve classic problems like FizzBuzz and password checks
The Three Operators — A Quick Refresher
| Operator | Plain English | Example |
|---|---|---|
| `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:
not(highest)andor(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:
- First check: divisible by 3 and 5? → print "FizzBuzz"
- Then check: divisible by 3 only? → print "Fizz"
- Then check: divisible by 5 only? → print "Buzz"
- 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
andwith helpfulnoterror 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!
Related Page
🔗Related Content
- 💻
Phase 1 - Practice Problems
Practice Phase 1 concepts with hands-on coding problems
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with assessment questions
- ➡️
Phase 2 - Get Started
Continue to Phase 2 after mastering Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum