Combining Multiple Logical Conditions

🏷️ Conditional Logic and Decision Making / Logical Operators

🧠 Context Introduction

In real-world programming, you rarely check just one condition at a time. Often, you need to evaluate multiple conditions together to make a decision. For example, a server alert should trigger only if the CPU usage is high and the memory is also low. Or a deployment should proceed if the tests pass or if it's a hotfix.

Python provides three logical operators to combine conditions: and, or, and not. These allow you to build complex decision-making logic from simple comparisons.


⚙️ The Three Logical Operators

Operator Meaning Behavior
and Both conditions must be True Returns True only if both sides are True
or At least one condition must be True Returns True if either side is True
not Reverses the condition Returns the opposite boolean value

🛠️ Using the and Operator

The and operator checks if both conditions are true at the same time.

Example scenario: A backup should run only if it's a weekday and the disk space is above 20%.

  • Condition 1: day == "weekday" → True
  • Condition 2: disk_space > 20 → True
  • Combined: day == "weekday" and disk_space > 20 → True

If either condition is False, the entire expression becomes False.

Practical example: - if status == "active" and error_count == 0: → Proceed with health check - if user_role == "admin" and login_attempts < 3: → Allow access


🕵️ Using the or Operator

The or operator checks if at least one condition is true.

Example scenario: Send an alert if CPU usage is above 90% or memory usage is above 95%.

  • Condition 1: cpu > 90 → False
  • Condition 2: memory > 95 → True
  • Combined: cpu > 90 or memory > 95 → True

Only one condition needs to be True for the whole expression to be True.

Practical example: - if response_code == 500 or response_code == 503: → Log as server error - if timeout_exceeded or connection_lost: → Retry the request


🔄 Using the not Operator

The not operator simply flips a boolean value. If something is True, not makes it False, and vice versa.

Example scenario: Execute a task only if the system is not in maintenance mode.

  • Condition: maintenance_mode = True
  • With not: not maintenance_mode → False
  • The task will not run because the condition is now False

Practical example: - if not is_connected: → Attempt reconnection - if not file_exists: → Create a new file


🧩 Combining Multiple Operators Together

You can chain and, or, and not in a single condition. Python follows a precedence order: not is evaluated first, then and, then or. Use parentheses to make your logic clear.

Example scenario: A deployment should proceed if: - The code passes all tests and - Either the environment is staging or the change is approved by a lead

Condition breakdown: - tests_passed and (environment == "staging" or approved_by_lead) - If tests_passed is False → whole condition is False (short-circuit) - If tests_passed is True → then check the part inside parentheses

Another example with not: - if not (error_count > 0 or warning_count > 5): → System is healthy - This means: proceed only if there are no errors and no more than 5 warnings


📊 Comparison Table: When to Use Each

Scenario Best Operator Why
Both conditions must be true and Strict requirement, no exceptions
Either condition can be true or Flexible, any match is enough
You need the opposite of a condition not Invert a check for cleaner logic
Complex business rules and + or with parentheses Combine multiple requirements clearly

🎯 Key Takeaways for Engineers

  • Use and when every condition must be satisfied (e.g., all checks pass)
  • Use or when any single condition is enough (e.g., any alert triggers)
  • Use not to invert a condition (e.g., skip if something is True)
  • Always use parentheses () when mixing operators to avoid confusion
  • Python stops evaluating as soon as it knows the result (short-circuit evaluation)

✅ Quick Reference

  • and → Both must be True
  • or → At least one must be True
  • not → Reverses the boolean value
  • Parentheses () → Group conditions for clarity
  • Short-circuit → Python stops early once the result is determined

By mastering these three logical operators, you can write conditions that accurately reflect real-world rules and automate decisions with confidence.


Combining multiple logical conditions lets you check several Boolean expressions at once using and, or, and not operators.


🔧 Example 1: Using and to check two conditions are both true

This example shows that and returns True only when every condition is True.

temperature = 75
humidity = 60

if temperature > 70 and humidity < 80:
    print("Comfortable weather")
else:
    print("Uncomfortable weather")

📤 Output: Comfortable weather


🔧 Example 2: Using or to check if at least one condition is true

This example shows that or returns True when any one condition is True.

is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("You can relax today")
else:
    print("It's a workday")

📤 Output: You can relax today


🔧 Example 3: Using not to invert a condition

This example shows that not flips True to False and False to True.

system_active = False

if not system_active:
    print("System is offline — starting now")
else:
    print("System is already running")

📤 Output: System is offline — starting now


🔧 Example 4: Combining and, or, and not together

This example shows how to mix all three logical operators in one condition.

speed = 55
is_raining = True
has_abs_brakes = False

if speed > 50 and is_raining and not has_abs_brakes:
    print("Warning: Slippery conditions — reduce speed")
else:
    print("Driving conditions are acceptable")

📤 Output: Warning: Slippery conditions — reduce speed


🔧 Example 5: Practical engineer check — system startup validation

This example shows a real-world scenario where multiple conditions must pass before a system starts.

power_ok = True
temperature_ok = True
pressure_ok = False
emergency_stop = False

if power_ok and temperature_ok and pressure_ok and not emergency_stop:
    print("System startup: All conditions met — starting")
else:
    print("System startup: Blocked — check conditions")

📤 Output: System startup: Blocked — check conditions


📊 Simple Comparison Table

Operator What it does Returns True when
and Both conditions must be true All conditions are True
or At least one condition must be true Any condition is True
not Flips the Boolean value The original condition is False

🧠 Context Introduction

In real-world programming, you rarely check just one condition at a time. Often, you need to evaluate multiple conditions together to make a decision. For example, a server alert should trigger only if the CPU usage is high and the memory is also low. Or a deployment should proceed if the tests pass or if it's a hotfix.

Python provides three logical operators to combine conditions: and, or, and not. These allow you to build complex decision-making logic from simple comparisons.


⚙️ The Three Logical Operators

Operator Meaning Behavior
and Both conditions must be True Returns True only if both sides are True
or At least one condition must be True Returns True if either side is True
not Reverses the condition Returns the opposite boolean value

🛠️ Using the and Operator

The and operator checks if both conditions are true at the same time.

Example scenario: A backup should run only if it's a weekday and the disk space is above 20%.

  • Condition 1: day == "weekday" → True
  • Condition 2: disk_space > 20 → True
  • Combined: day == "weekday" and disk_space > 20 → True

If either condition is False, the entire expression becomes False.

Practical example: - if status == "active" and error_count == 0: → Proceed with health check - if user_role == "admin" and login_attempts < 3: → Allow access


🕵️ Using the or Operator

The or operator checks if at least one condition is true.

Example scenario: Send an alert if CPU usage is above 90% or memory usage is above 95%.

  • Condition 1: cpu > 90 → False
  • Condition 2: memory > 95 → True
  • Combined: cpu > 90 or memory > 95 → True

Only one condition needs to be True for the whole expression to be True.

Practical example: - if response_code == 500 or response_code == 503: → Log as server error - if timeout_exceeded or connection_lost: → Retry the request


🔄 Using the not Operator

The not operator simply flips a boolean value. If something is True, not makes it False, and vice versa.

Example scenario: Execute a task only if the system is not in maintenance mode.

  • Condition: maintenance_mode = True
  • With not: not maintenance_mode → False
  • The task will not run because the condition is now False

Practical example: - if not is_connected: → Attempt reconnection - if not file_exists: → Create a new file


🧩 Combining Multiple Operators Together

You can chain and, or, and not in a single condition. Python follows a precedence order: not is evaluated first, then and, then or. Use parentheses to make your logic clear.

Example scenario: A deployment should proceed if: - The code passes all tests and - Either the environment is staging or the change is approved by a lead

Condition breakdown: - tests_passed and (environment == "staging" or approved_by_lead) - If tests_passed is False → whole condition is False (short-circuit) - If tests_passed is True → then check the part inside parentheses

Another example with not: - if not (error_count > 0 or warning_count > 5): → System is healthy - This means: proceed only if there are no errors and no more than 5 warnings


📊 Comparison Table: When to Use Each

Scenario Best Operator Why
Both conditions must be true and Strict requirement, no exceptions
Either condition can be true or Flexible, any match is enough
You need the opposite of a condition not Invert a check for cleaner logic
Complex business rules and + or with parentheses Combine multiple requirements clearly

🎯 Key Takeaways for Engineers

  • Use and when every condition must be satisfied (e.g., all checks pass)
  • Use or when any single condition is enough (e.g., any alert triggers)
  • Use not to invert a condition (e.g., skip if something is True)
  • Always use parentheses () when mixing operators to avoid confusion
  • Python stops evaluating as soon as it knows the result (short-circuit evaluation)

✅ Quick Reference

  • and → Both must be True
  • or → At least one must be True
  • not → Reverses the boolean value
  • Parentheses () → Group conditions for clarity
  • Short-circuit → Python stops early once the result is determined

By mastering these three logical operators, you can write conditions that accurately reflect real-world rules and automate decisions with confidence.

Interactive Views

You are currently in 📚 All-in-One mode. Use the tabs at the top to switch to 📖 Theory Only or 💻 Code Only views.

Combining multiple logical conditions lets you check several Boolean expressions at once using and, or, and not operators.


🔧 Example 1: Using and to check two conditions are both true

This example shows that and returns True only when every condition is True.

temperature = 75
humidity = 60

if temperature > 70 and humidity < 80:
    print("Comfortable weather")
else:
    print("Uncomfortable weather")

📤 Output: Comfortable weather


🔧 Example 2: Using or to check if at least one condition is true

This example shows that or returns True when any one condition is True.

is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("You can relax today")
else:
    print("It's a workday")

📤 Output: You can relax today


🔧 Example 3: Using not to invert a condition

This example shows that not flips True to False and False to True.

system_active = False

if not system_active:
    print("System is offline — starting now")
else:
    print("System is already running")

📤 Output: System is offline — starting now


🔧 Example 4: Combining and, or, and not together

This example shows how to mix all three logical operators in one condition.

speed = 55
is_raining = True
has_abs_brakes = False

if speed > 50 and is_raining and not has_abs_brakes:
    print("Warning: Slippery conditions — reduce speed")
else:
    print("Driving conditions are acceptable")

📤 Output: Warning: Slippery conditions — reduce speed


🔧 Example 5: Practical engineer check — system startup validation

This example shows a real-world scenario where multiple conditions must pass before a system starts.

power_ok = True
temperature_ok = True
pressure_ok = False
emergency_stop = False

if power_ok and temperature_ok and pressure_ok and not emergency_stop:
    print("System startup: All conditions met — starting")
else:
    print("System startup: Blocked — check conditions")

📤 Output: System startup: Blocked — check conditions


📊 Simple Comparison Table

Operator What it does Returns True when
and Both conditions must be true All conditions are True
or At least one condition must be true Any condition is True
not Flips the Boolean value The original condition is False