Logical Operators: AND, OR, NOT
What You'll Learn
You'll learn how to combine multiple boolean expressions using and, or, and not to create more powerful compound conditions.
The Core Idea
Sometimes a single comparison isn't enough. What if you need to check two conditions at once—like whether a user is both logged in AND an admin? Or whether it's a weekend (Saturday OR Sunday)? That's where logical operators come in.
The Three Logical Operators
and — Both conditions must be true
age >= 18 and hasLicense == true— Only true if BOTH are true- Think: "You need a ticket AND an ID to enter"
or — At least one condition must be true
day == "Saturday" or day == "Sunday"— True if EITHER is true- Think: "Pay with cash OR card"
not — Reverses a boolean value
not isRaining— True ifisRainingis false- Think: "NOT allowed" means forbidden
Combining Them
You can chain these together:
if age >= 13 and age <= 19 and not hasParentalConsent:
# teenager without consent
This checks three things at once: age is at least 13, age is at most 19, and they don't have parental consent.
Truth in Action
true and true→truetrue and false→falsetrue or false→truefalse or false→falsenot true→false
When to Use
Use logical operators whenever your decision depends on multiple facts being true or false together, rather than just one condition.
Key Takeaway: Logical operators (and, or, not) let you combine simple boolean expressions into compound conditions that make more complex decisions possible.