This guide explains Logical Operators: and, or, and not through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
and, or, and not combine conditions. and requires both operands to be truthy, or requires at least one, and not produces the opposite boolean value. and and or can return one of their operands rather than a new Boolean.
When You Would Use This
A basic entry check requires a ticket and an acceptable age.
Step-by-Step Example
has_ticket = True
visitor_age = 17
is_adult = visitor_age >= 18
print(has_ticket and is_adult)
print(has_ticket or is_adult)
print(not has_ticket)
How the Example Works
The outputs are False, True, and False. A combined condition is easier to read when each subcondition has a meaningful name.
Example Output
False
True
False
Try It Yourself
Permit entry when a visitor has a ticket and is at least 16, or has a staff pass.
Common Beginner Mistakes
- Using
&in place ofandwith ordinary boolean decisions. - Assuming
andandoralways return aboolrather than sometimes returning an operand. - Leaving out parentheses in complicated mixed conditions.
Key Takeaways
and,or, andnotcombine conditions.- The outputs are
False,True, andFalse. - Avoid this mistake: Using
&in place ofandwith ordinary boolean decisions.