Lesson 8 of 21 · 2 min
Logical Operators — Combining Conditions
The WHY — Why Combine Conditions?
You often need to check multiple things at once. "Is the student enrolled AND did they pass?" needs both conditions to be true. Logical operators let you combine conditions.
| Operator | Meaning | Result is True when... |
|---|---|---|
and | Both must be True | BOTH conditions are True |
or | At least one must be True | AT LEAST ONE condition is True |
not | Flips True to False | The original condition was False |
and — Both Conditions Must Be True
age = 22 # person's age
has_degree = True # whether they have a degree
# AND: BOTH must be True for the job application to proceed
if age >= 21 and has_degree:
print("Application accepted") # only runs if BOTH are True
else:
print("Not eligible")
Output: Application accepted
or — At Least One Condition Must Be True
is_student = True # is this person a student?
is_senior = False # is this person 65 or older?
# OR: either condition being True qualifies for the discount
if is_student or is_senior:
print("Discount applied: 20% off") # runs if EITHER is True
else:
print("No discount available")
Output: Discount applied: 20% off
not — Flips True to False and False to True
is_raining = False # current weather status
# NOT: flips the condition
if not is_raining: # "if it is NOT raining"
print("Great day for a walk!") # runs because not False = True
else:
print("Take an umbrella.")
Output: Great day for a walk!
Combining Multiple Logical Operators
score = 72 # exam score
attendance = 80 # attendance percentage
# Student passes if: score >= 60 AND attendance >= 75
if score >= 60 and attendance >= 75:
print("Pass — meets both requirements") # both conditions are True
elif score >= 60 or attendance >= 75:
print("Review case — meets one requirement") # at least one is True
else:
print("Fail — meets neither requirement") # neither is True
Output: Pass — meets both requirements
