What If One Thing Is True and the Other Isn't?
The problem...
You know how to check one condition.
Is the age above 18? Is the price below 100?
But real programs rarely check just one thing at a time.
What if you need both to be true? What if only one needs to be true? What if you need the opposite?
The idea!
Python has three logical operators that let you combine or modify conditions.
and— both must be trueor— at least one must be truenot— flips true to false and false to true
Making it real — and
You want to enter a club. You need to be over 18 AND have an ID.
age = 20
has_id = True
print(age > 18 and has_id) # True — both conditions met
If either condition is false, the result is false.
age = 16
has_id = True
print(age > 18 and has_id) # False — age condition fails
Making it real — or
You get a discount if you're a student OR a senior.
is_student = False
is_senior = True
print(is_student or is_senior) # True — at least one is true
Only if both are false, the result is false.
is_student = False
is_senior = False
print(is_student or is_senior) # False — neither is true
Making it real — not
You want to check if something is NOT true.
is_banned = False
print(not is_banned) # True — flipped
is_banned = True
print(not is_banned) # False — flipped
In practice
age = 20
has_id = True
is_banned = False
print(age > 18 and has_id and not is_banned) # True
All three conditions in one line. Python reads left to right.
Going further
Logical operators can be combined with comparison operators freely.
price = 85
is_member = True
print(price < 100 or is_member) # True
print(price < 50 and is_member) # False
print(not is_member or price < 50) # False
What's really happening
Logical operators take bool values and return a bool value.
You're not doing math. You're building logic — and logic is the foundation of every decision a program makes.
Heads up!
andreturnsTrueonly if BOTH sides are trueorreturnsTrueif AT LEAST ONE side is truenotflips the value —not TrueisFalse,not FalseisTrue- You can chain multiple conditions — Python reads left to right
The mindset shift
Stop thinking: "I can only check one thing at a time."
Start thinking: "I can combine conditions to express any logic I need."
What you should understand now
and— both must be trueor— at least one must be truenot— flips the bool value- Logical operators always return a bool