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.
and to require multiple conditionsor to accept alternativesnot to flip a conditionThe and Operator
and requires both conditions to be True. If either one is False, the whole expression is False:
Think of and as a strict bouncer β both conditions must pass:
| A | B | Result | |
|---|---|---|---|
| True | and | True | True |
| True | and | False | False |
| False | and | True | False |
| False | and | False | False |
| A | B | Result | |
|---|---|---|---|
| True | or | True | True |
| True | or | False | True |
| False | or | True | True |
| False | or | False | False |
The or Operator
or only needs one condition to be True. It's only False when both are False:
The not Operator
not flips a boolean β True becomes False, False becomes True:
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:
Real-World Example: Leap Years
The leap year algorithm is a classic example of compound conditions. A year is a leap year if:
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!
Challenges
Challenge 1: Ride Check
A roller coaster requires riders to be at least 48 inches tall AND at least 8 years old.
β’ True if height >= 48 AND age >= 8
β’ False otherwise
Then set message to "Enjoy the ride!" or "Sorry, you can't ride yet."
height >= 48 and age >= 8Hint 2: You can set can_ride first, then use it in the if:
can_ride = height >= 48 and age >= 8if can_ride:Hint 3: Or do it all in one if:
if height >= 48 and age >= 8:
Challenge 2: Leap Year
Implement the leap year algorithm using logical operators.
β’ 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)
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
Build a login checker with multiple conditions and helpful error messages.
β’ "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 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
Use the Triangle Inequality Theorem to check if three sides can form a valid triangle, and if so, what type.
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"
and three times: a + b > c and a + c > b and b + c > aHint 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.