Logical Operators — Combining Conditions
You've learned to ask simple True/False questions with comparison operators. Now it's time to ask more complex questions. "Is this value between 5 and 10?" That's not one comparison—it's two comparisons combined: "Is it greater than 5 AND less than 10?"
Logical operators let you combine True/False values (or comparisons) into more sophisticated reasoning. They're the foundation for all decision-making in Chapter 22's control flow.
What It Is: Combining True/False Values
A logical operator combines two True/False conditions into a single True/False result. There are three logical operators:
and— True only if both conditions are Trueor— True if at least one condition is Truenot— Reverses the value (True becomes False, False becomes True)
With these three operators, you can express complex logic: "Is the user logged in AND has been a member for 7+ days?" or "Does the password have 8+ characters OR has the user verified their email?"
The Three Logical Operators
AND: Both Conditions Must Be True
# AND operator: both must be True
condition1: bool = True
condition2: bool = False
and_result: bool = condition1 and condition2 # False (one is False)
print(f"True and False = {and_result}") # False
# Another example
condition3: bool = True
condition4: bool = True
and_result2: bool = condition3 and condition4 # True (both are True)
print(f"True and True = {and_result2}") # True
# Truth table for AND
print("AND Truth Table:")
print(f"True and True = {True and True}") # True
print(f"True and False = {True and False}") # False
print(f"False and True = {False and True}") # False
print(f"False and False = {False and False}") # False
The and operator has a simple rule: it returns True only if both conditions are True. If even one is False, the whole expression is False. This is useful for checking multiple requirements.
💬 AI Colearning Prompt
"Why does Python short-circuit evaluation with
and? In other words, if the first condition is False, why does Python stop checking the second condition? What's the advantage?"
This question digs into how Python optimizes logical evaluation—a valuable understanding for later performance considerations.
OR: At Least One Condition Must Be True
# OR operator: at least one must be True
condition1: bool = True
condition2: bool = False
or_result: bool = condition1 or condition2 # True (one is True)
print(f"True or False = {or_result}") # True
# Another example
condition3: bool = False
condition4: bool = False
or_result2: bool = condition3 or condition4 # False (both are False)
print(f"False or False = {or_result2}") # False
# Truth table for OR
print("OR Truth Table:")
print(f"True or True = {True or True}") # True
print(f"True or False = {True or False}") # True
print(f"False or True = {False or True}") # True
print(f"False or False = {False or False}") # False
The or operator returns True if at least one condition is True. It's False only when both are False. Use or when you want to check if any of multiple conditions is satisfied.
🎓 Expert Insight
In AI-native development, you don't memorize truth tables. You think about the real-world logic: "AND means both must be satisfied; OR means either one is enough." Once you have that mental model, the logic follows naturally. If you get confused, ask AI: "Should I use and or or here?" and explain your logic.
NOT: Reversing a Value
# NOT operator: flips the value
condition: bool = True
not_result: bool = not condition # False (True becomes False)
print(f"not True = {not_result}") # False
condition2: bool = False
not_result2: bool = not condition2 # True (False becomes True)
print(f"not False = {not_result2}") # True
# Combining NOT with other operators
is_admin: bool = False
is_not_admin: bool = not is_admin # True
print(f"Is not admin: {is_not_admin}") # True
The not operator is the simplest: it just flips the value. If the condition is True, not condition is False. If it's False, not condition is True.
🤝 Practice Exercise
Ask your AI: "Create a truth table showing all combinations of
and,or, andnotoperators. Then explain: What's the relationship betweennot (x and y)and(not x) or (not y)? Why does this matter?"
Expected Outcome: You'll see that logical operators follow mathematical laws (De Morgan's Laws); understand that different expressions can be logically equivalent; see how logic works systematically, not randomly.
Combining Comparisons with Logical Operators
Now let's combine what you learned in Lesson 2 (comparisons) with logical operators.
Checking if a Value is in a Range
# Is x between 5 and 10?
x: int = 7
in_range: bool = (x > 5) and (x < 10) # True (both conditions are met)
print(f"Is {x} between 5 and 10? {in_range}") # True
# Note: We use parentheses for clarity (not required, but helpful)
# Is x OUTSIDE the range [5, 10]?
out_of_range: bool = (x <= 5) or (x >= 10) # False (neither condition is met)
print(f"Is {x} outside [5, 10]? {out_of_range}") # False
# Is x NOT in the range?
not_in_range: bool = not ((x > 5) and (x < 10)) # False (it IS in range)
print(f"Is {x} NOT in range? {not_in_range}") # False
These are the kinds of conditions you'll use constantly in Chapter 22 when writing if statements.
Real-World Permission Logic
# Permission check: User can post if logged in AND account is verified
is_logged_in: bool = True
account_verified: bool = False
can_post: bool = is_logged_in and account_verified # False (verification missing)
print(f"Can post: {can_post}") # False
# Alternative permission: Admin OR approved user
is_admin: bool = False
is_approved_user: bool = True
can_manage: bool = is_admin or is_approved_user # True (approved user is enough)
print(f"Can manage content: {can_manage}") # True
# Complex condition: (Admin OR Moderator) AND Account Active
is_moderator: bool = True
account_active: bool = True
can_moderate: bool = (is_admin or is_moderator) and account_active # True
print(f"Can moderate: {can_moderate}") # True
Notice how parentheses control the order of evaluation. (is_admin or is_moderator) and account_active evaluates the OR part first, then checks if the account is active.
Evaluation Order Matters
When you combine multiple logical operators, Python evaluates them in a specific order: not first, then and, then or.
# Without explicit parentheses, Python follows operator precedence
result: bool = True or False and False # What's the result?
# Python evaluates: (True or (False and False))
# False and False = False
# True or False = True
print(f"True or False and False = {result}") # True
# With explicit parentheses, we control the order
result2: bool = (True or False) and False # Different result
# (True or False) = True
# True and False = False
print(f"(True or False) and False = {result2}") # False
This is why using parentheses is smart—even when not required, they make your intent clear to anyone reading your code (including yourself three months from now).
Try With AI
Ready to combine conditions with and, or, and not operators?
🔍 Explore Logical Operator Behavior:
"Explain and, or, and not operators with truth tables. For each operator, show me 3 examples combining boolean values and explain the result. Why does 'True and False' return False but 'True or False' returns True? Include short-circuit evaluation: does 'False and expensive_function()' call the function?"
🎯 Practice Access Control Logic:
"Create access control for premium content. Users can access if: (is_subscriber AND is_active) OR is_admin. Test these 3 cases: (1) subscriber=True, active=True, admin=False, (2) subscriber=False, active=False, admin=True, (3) subscriber=True, active=False, admin=False. Show the step-by-step evaluation. Then compare to this modified rule: (is_subscriber OR is_admin) AND is_active. How do results change?"
🧪 Test Operator Precedence:
"Compare these two rules for an admin with inactive account (subscriber=False, active=False, admin=True): Rule A: is_subscriber AND is_active OR is_admin, Rule B: (is_subscriber OR is_admin) AND is_active. Which grants access and which denies? Show me how parentheses change the evaluation order. Which rule is more secure for access control?"
🚀 Apply to Your Business Logic:
"I'm building [describe your system]. Help me write multi-condition logic for: user permissions, eligibility checks, validation rules, or workflow conditions. For each rule, combine conditions with and, or, not. Add parentheses to make precedence clear. Explain why each condition is necessary."