Unit 2 Β· Lesson 4

Logical Operators

Real decisions are rarely simple. "Can you ride?" depends on height and age. "Is it a holiday?" means Saturday or Sunday. Time to combine conditions.

Use and to require multiple conditions
Use or to accept alternatives
Use not to flip a condition
Build compound boolean expressions

The and Operator

and requires both conditions to be True. If either one is False, the whole expression is False:

age = 25 has_license = True if age >= 16 and has_license: print("You can drive!") else: print("You cannot drive.")

Think of and as a strict bouncer β€” both conditions must pass:

and β€” both must be True
ABResult
TrueandTrueTrue
TrueandFalseFalse
FalseandTrueFalse
FalseandFalseFalse
or β€” at least one must be True
ABResult
TrueorTrueTrue
TrueorFalseTrue
FalseorTrueTrue
FalseorFalseFalse

The or Operator

or only needs one condition to be True. It's only False when both are False:

day = "Saturday" if day == "Saturday" or day == "Sunday": print("It's the weekend!") else: print("It's a weekday.")
⚠️ Common mistake: Writing day == "Saturday" or "Sunday" does NOT work as expected! Python reads that as (day == "Saturday") or ("Sunday"), and since the string "Sunday" is truthy, it's always True. You must write the full comparison on each side of or.

The not Operator

not flips a boolean β€” True becomes False, False becomes True:

is_raining = False if not is_raining: print("No umbrella needed!") # This runs

not is great for readability. Compare: if not is_raining reads like English, while if is_raining == False is clunky.

Combining All Three

You can mix and, or, and not in the same expression. Use parentheses to make the logic clear:

age = 20 is_student = True is_member = False # Discount if: (student AND under 25) OR member if (is_student and age < 25) or is_member: print("You get a discount!") # True β€” student and under 25
Operator precedence: Python evaluates not first, then and, then or. But don't memorize that β€” just use parentheses to make your intent crystal clear!

Real-World Example: Leap Years

The leap year algorithm is a classic example of compound conditions. A year is a leap year if:

Rule: Divisible by 4, except years divisible by 100, unless they're also divisible by 400.

So 2024 is a leap year (Γ·4 βœ“), 1900 is NOT (Γ·100 but not Γ·400), and 2000 IS (Γ·400 βœ“).

You'll implement this yourself in Challenge 2!

Practice Time

Challenges

🎒

Challenge 1: Ride Check

Guided

A roller coaster requires riders to be at least 48 inches tall AND at least 8 years old.

Instructions: Using and, write an if/else that sets can_ride:
β€’ True if height >= 48 AND age >= 8
β€’ False otherwise

Then set message to "Enjoy the ride!" or "Sorry, you can't ride yet."
Hint 1: You need both conditions to be true: height >= 48 and age >= 8
Hint 2: You can set can_ride first, then use it in the if:
can_ride = height >= 48 and age >= 8
if can_ride:
Hint 3: Or do it all in one if: if height >= 48 and age >= 8:
πŸ“…

Challenge 2: Leap Year

Guided

Implement the leap year algorithm using logical operators.

Instructions: Set is_leap to True or False based on the rules:
β€’ Divisible by 4 β†’ leap year
β€’ EXCEPT if divisible by 100 β†’ not a leap year
β€’ UNLESS also divisible by 400 β†’ leap year again

You can do this with a single boolean expression:
(divisible by 4 and not divisible by 100) or (divisible by 400)
Hint 1: "Divisible by 4" means year % 4 == 0. Same pattern for 100 and 400.
Hint 2: The full expression: (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
Hint 3: Test in your head: 2024 β†’ Γ·4 yes, Γ·100 no β†’ leap! 1900 β†’ Γ·4 yes, Γ·100 yes, Γ·400 no β†’ not leap. 2000 β†’ Γ·400 yes β†’ leap!
πŸ”‘

Challenge 3: Login System

Solo

Build a login checker with multiple conditions and helpful error messages.

Instructions: Check the username and password against the stored credentials. Set message to:
β€’ "Welcome back, admin!" β€” if both username AND password are correct
β€’ "Incorrect password." β€” if username is correct but password is wrong
β€’ "User not found." β€” if username is wrong (regardless of password)
Hint 1: Think about the order of checks. Check the most specific case first (both correct), then username correct but password wrong, then everything else.
Hint 2: if username == correct_user and password == correct_pass: β†’ welcome. elif username == correct_user: β†’ wrong password. else: β†’ user not found.
Hint 3: The elif only runs if the if was False, so you already know both aren't correct. If the username matches, the password must be wrong!
πŸ“

Challenge 4: Triangle Validator

Stretch

Use the Triangle Inequality Theorem to check if three sides can form a valid triangle, and if so, what type.

Instructions: Given three side lengths, determine:
1. Set is_valid β€” True if all three triangle inequality conditions hold: every side must be less than the sum of the other two (a+b>c AND a+c>b AND b+c>a)
2. If valid, set triangle_type:
  β€’ "equilateral" β€” all 3 sides equal
  β€’ "isosceles" β€” exactly 2 sides equal
  β€’ "scalene" β€” no sides equal
3. If not valid, set triangle_type to "invalid"
Hint 1: The validity check uses and three times: a + b > c and a + c > b and b + c > a
Hint 2: For equilateral: a == b and b == c. For isosceles: a == b or b == c or a == c (but not equilateral!).
Hint 3: Check validity first with if. Inside the valid branch, use if/elif/else for equilateral β†’ isosceles β†’ scalene. Put the invalid case in the outer else.