Skip to main content

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 17'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 True
  • or — True if at least one condition is True
  • not — 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.

🎓 Instructor Commentary

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.

🚀 CoLearning Challenge

Ask your AI Co-Teacher:

"Create a truth table showing all combinations of and, or, and not operators. Then explain: What's the relationship between not (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 17 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.

✨ Teaching Tip

Use your AI tool to explore operator precedence: "Evaluate this step-by-step: not (True or False) and True. What's the result? Show me the evaluation order and explain why not has higher precedence than and and or."

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

Now it's your turn to explore logical operators with an AI co-teacher. These prompts build from understanding to application to edge cases to synthesis.

Tool Choice: Use Claude Code, Gemini CLI, or ChatGPT web—whatever you have access to.

Prompt 1: Concept Exploration (Understand)

Copy and ask your AI:

"I'm learning logical operators in Python. I'm confused about and vs. or.

  • When does and return True?
  • When does or return True?
  • What does not do?
  • Why do we need all three operators?

Give me clear examples for each that make the difference obvious."

Expected Outcome: You'll understand the truth conditions for each operator; see practical distinctions (and requires both, or needs only one, not reverses); grasp fundamental boolean logic.


Prompt 2: Application (Apply)

Copy and ask your AI:

"Write code for a permission system:

  • A user can post to the forum if:
    • They are logged in AND they've been a member for 7+ days

Write the comparison and logical operator logic.

Then modify it: What if ANY of these are true?

  • User is an admin, OR
  • User is a member for 7+ days

Show me both versions with clear variable names."

Expected Outcome: You'll apply logical operators to realistic permission logic; understand difference between and (both required) and or (either sufficient); see practical value of combining operators.


Prompt 3: Edge Case Discovery (Explore)

Copy and ask your AI:

"Evaluate these step-by-step and explain the result:

  • True and False or True
  • not (True or False)
  • (True and False) or (True and True)
  • not True and False

Which ones are True? Which are False? Show the evaluation order for each one."

Expected Outcome: You'll practice evaluating complex boolean expressions; learn operator precedence (not > and > or); discover that parentheses matter; understand how to trace through logic systematically.


Prompt 4: Synthesis & Validation (Understand + Analyze)

Copy and ask your AI:

"I see how logical operators combine conditions. But why teach them in Chapter 15 when Chapter 17 teaches if statements?

  • How do logical operators prepare me for writing if statements like: if age >= 18 and has_license:
  • Why not just teach 'if' statements directly and mention logical operators there?
  • What's the benefit of learning operators separately?

Connect logical operators to control flow in Chapter 17."

Expected Outcome: You'll understand that logical operators (Boolean logic) are the foundation for conditional statements in Chapter 17; see that if statements use the exact same logic you're learning now; appreciate the learning progression (operators first, then control flow that uses them).


Safety Note: Logical operators are deterministic—once you understand the rules, you can predict results. If you get stuck on evaluation order, use parentheses liberally. Clear logic is better than clever logic.