Skip to main content

Lesson 15: Combining Conditions (and, or, not)

Learn how to use and, or, and not operators, combine conditions with parentheses, and apply common validation patterns in Python effectively.

Introduction

In the last lesson, you learned to check single conditions. But real-world validation rarely involves just one check.

A room isn't just compliant because it meets the area requirement. It also needs adequate ceiling height, proper ventilation, and fire safety measures.

A sheet isn't ready for issue just because it's marked "Approved." It also needs to be reviewed, have no outstanding comments, and match the current design intent.

This is where logical operators come in. They let you combine multiple conditions into one check.

Python provides three logical operators: and, or, and not.


Lesson Overview

This section contains a general overview of topics you will learn in this lesson.

  • Using and to require all conditions
  • Using or to require at least one condition
  • Using not to reverse a condition
  • Combining operators with parentheses
  • Common validation patterns

The and Operator

Use and when ALL conditions must be true.

room_area = 420
ceiling_height = 2.8

if room_area >= 400 and ceiling_height >= 2.7:
    print("Room is compliant")

Both conditions must be true for the code to run:

  • room_area >= 400 must be True
  • ceiling_height >= 2.7 must be True

If either is false, the entire condition is false.


Truth Table for and

True  and True  = True
True  and False = False
False and True  = False
False and False = False

All conditions must be true. One false condition makes the whole thing false.


Architectural Example: Room Compliance

room_area = 420
ceiling_height = 2.8
has_windows = True

minimum_area = 400
minimum_height = 2.7

if room_area >= minimum_area and ceiling_height >= minimum_height and has_windows:
    print("Room meets all habitability requirements")
else:
    print("Room does not meet requirements")

All three conditions must be true:

  • Area >= 400
  • Height >= 2.7
  • Has windows

If any one fails, the room is non-compliant.


The or Operator

Use or when AT LEAST ONE condition must be true.

has_fire_door = False
has_sprinklers = True
has_fire_alarm = True

if has_fire_door or has_sprinklers or has_fire_alarm:
    print("Fire safety requirement met")

At least one condition must be true. If any one is true, the entire condition is true.


Truth Table for or

True  or True  = True
True  or False = True
False or True  = True
False or False = False

Only one condition needs to be true. All must be false for the result to be false.


Architectural Example: Fire Safety

has_fire_door = False
has_sprinklers = True
has_fire_alarm = False

if has_fire_door or has_sprinklers or has_fire_alarm:
    print("Fire safety: Compliant")
else:
    print("Fire safety: Non-compliant - needs at least one safety measure")

# Output: Fire safety: Compliant (sprinklers = True)

The building needs at least one fire safety measure. It has sprinklers, so it's compliant.


The not Operator

Use not to reverse a boolean value.

is_basement = False

if not is_basement:
    print("Floor is above ground")

not flips the value:

  • not True becomes False
  • not False becomes True

Truth Table for not

not True  = False
not False = True

Architectural Example: Floor Eligibility

is_basement = False
is_mechanical = False

if not is_basement and not is_mechanical:
    print("Floor is habitable")
else:
    print("Floor is not habitable")

# Output: Floor is habitable

The floor is habitable if it's NOT a basement AND NOT a mechanical floor.


Combining Multiple Operators

You can combine and, or, and not in the same condition.

room_area = 420
ceiling_height = 2.8
is_basement = False
has_windows = True

if room_area >= 400 and ceiling_height >= 2.7 and not is_basement and has_windows:
    print("Room is habitable")

All of these must be true:

  • Area >= 400
  • Height >= 2.7
  • NOT basement
  • Has windows

Operator Precedence

When combining operators, Python evaluates them in this order:

  1. not (highest priority)
  2. and
  3. or (lowest priority)
result = True or False and False
print(result)  # Output: True

This evaluates as: True or (False and False)True or FalseTrue

Best practice: Use parentheses to make your intent clear, even if they're not strictly necessary.

result = True or (False and False)  # Explicit

Using Parentheses for Clarity

Parentheses let you control the order of evaluation and make complex conditions more readable.

room_area = 420
is_corner_unit = True
has_windows = False

# Without parentheses (confusing)
if room_area >= 400 and is_corner_unit or has_windows:
    print("Acceptable")

# With parentheses (clear)
if (room_area >= 400 and is_corner_unit) or has_windows:
    print("Acceptable")

The first version is ambiguous. The second version is explicit: area AND corner unit, OR has windows.


Common Patterns

Pattern 1: Range Checking (Between Min and Max)

room_area = 450
min_area = 400
max_area = 600

if room_area >= min_area and room_area <= max_area:
    print("Area is within acceptable range")

You can also use chained comparisons (from Module 02):

if min_area <= room_area <= max_area:
    print("Area is within acceptable range")

Both work. The chained version is more concise.


Pattern 2: Exclusion (Not This and Not That)

floor_type = "Residential"

if floor_type != "Basement" and floor_type != "Mechanical":
    print("Floor is habitable")

Or using not in with a list:

excluded_types = ["Basement", "Mechanical", "Parking"]

if floor_type not in excluded_types:
    print("Floor is habitable")

The second approach scales better if you have many exclusions.


Pattern 3: At Least One Required Feature

has_elevator = True
has_ramp = False
has_ground_access = False

if has_elevator or has_ramp or has_ground_access:
    print("Building is accessible")
else:
    print("Building requires accessibility improvements")

Pattern 4: All Requirements Must Be Met

has_permits = True
design_approved = True
budget_approved = True
site_ready = False

if has_permits and design_approved and budget_approved and site_ready:
    print("Ready to begin construction")
else:
    print("Not ready - check requirements")

# Output: Not ready - check requirements (site_ready is False)

Real Architectural Workflows

Example 1: Complex Room Validation

room_area = 420
ceiling_height = 2.8
has_windows = True
is_basement = False
has_egress = True

minimum_area = 400
minimum_height = 2.7

# All conditions must be met
is_habitable = (
    room_area >= minimum_area and
    ceiling_height >= minimum_height and
    has_windows and
    not is_basement and
    has_egress
)

if is_habitable:
    print("Room is habitable")
    print("Approved for occupancy")
else:
    print("Room does not meet habitability requirements")

    # Detailed feedback
    if room_area < minimum_area:
        print(f"  - Area too small: {room_area}m² (minimum: {minimum_area}m²)")
    if ceiling_height < minimum_height:
        print(f"  - Height too low: {ceiling_height}m (minimum: {minimum_height}m)")
    if not has_windows:
        print("  - No windows")
    if is_basement:
        print("  - Basement location not allowed")
    if not has_egress:
        print("  - No emergency egress")

Example 2: Sheet Readiness Check

design_complete = True
reviewed = True
no_open_comments = False
matches_specs = True

ready_for_issue = (
    design_complete and
    reviewed and
    no_open_comments and
    matches_specs
)

if ready_for_issue:
    print("Sheet is ready for issue")
else:
    print("Sheet is not ready for issue:")

    if not design_complete:
        print("  - Design incomplete")
    if not reviewed:
        print("  - Not reviewed")
    if not no_open_comments:
        print("  - Has open comments")
    if not matches_specs:
        print("  - Does not match specifications")

# Output:
# Sheet is not ready for issue:
#   - Has open comments

Example 3: Priority Assignment

severity = "High"
affects_construction = True
deadline_passed = False

# Critical if severity is high AND affects construction OR deadline passed
is_critical = (severity == "High" and affects_construction) or deadline_passed

if is_critical:
    priority = 1
    response_time = "24 hours"
else:
    priority = 2
    response_time = "3 days"

print(f"Priority: {priority}, Response time: {response_time}")
# Output: Priority: 1, Response time: 24 hours

Common Mistakes

Using and when you mean or

# Wrong - this is never true
if room_type == "Office" and room_type == "Meeting":
    print("Conference space")

A room can't be both "Office" AND "Meeting" at the same time. You probably meant or:

if room_type == "Office" or room_type == "Meeting":
    print("Workspace")

Forgetting parentheses in complex conditions

# Ambiguous
if area >= 400 and height >= 2.7 or has_windows:
    print("Compliant")

# Clear
if (area >= 400 and height >= 2.7) or has_windows:
    print("Compliant")

The second version makes it obvious: (area AND height) OR windows.


Double negatives

# Confusing
if not not is_approved:
    print("Approved")

# Clear
if is_approved:
    print("Approved")

Avoid double negatives. They make code hard to read.


Checking the same thing multiple times

# Redundant
if area >= 400 and area > 300:
    print("Large room")

If area >= 400 is true, then area > 300 is automatically true. The second check is redundant.


Assignment

<div class="lesson-content__panel" markdown="1">

  1. Create a new file called combining_conditions.py
  2. Room habitability checker:
    • Variables: area = 420, height = 2.8, windows = True
    • Minimums: min_area = 400, min_height = 2.7
    • Check if ALL requirements are met
    • Print "Habitable" or "Not habitable"
  3. Fire safety validator:
    • Variables: fire_door = False, sprinklers = True, alarm = False
    • At least ONE must be true
    • Print "Compliant" or "Non-compliant"
  4. Floor eligibility:
    • Variables: floor_number = 3, is_mechanical = False
    • Floor is habitable if: floor >= 0 AND NOT mechanical
    • Print result
  5. Range validation:
    • Variable: room_area = 520
    • Valid range: 400 to 600
    • Use AND to check both min and max
    • Print "Within range" or "Out of range"
  6. Complex project readiness:
    • Variables: permits = True, design_done = True, budget_ok = False, site_ready = True
    • Project can start if ALL are true
    • Print "Ready to start" or "Not ready"
    • If not ready, print which conditions failed
  7. Priority assignment:
    • Variables: severity = "High", urgent = True
    • Critical if: severity == "High" AND urgent
    • Print priority level
  8. Exclusion check:
    • Variable: floor_type = "Residential"
    • Excluded types: "Basement", "Mechanical", "Parking"
    • Check if floor_type is NOT in excluded list
    • Print "Allowed" or "Not allowed"
  9. Experiment:
    • Write a condition with and where both are true
    • Write a condition with and where one is false (see result)
    • Write a condition with or where both are false
    • Use parentheses to change the order of evaluation

</div>


Knowledge Check

The following questions are an opportunity to reflect on key topics in this lesson.

  • <a class="knowledge-check-link" href="#the-and-operator">When does an and condition return True?</a>
  • <a class="knowledge-check-link" href="#the-or-operator">When does an or condition return True?</a>
  • <a class="knowledge-check-link" href="#the-not-operator">What does not do to a boolean value?</a>
  • <a class="knowledge-check-link" href="#operator-precedence">Which operator has the highest priority: and, or, or not?</a>
  • <a class="knowledge-check-link" href="#using-parentheses-for-clarity">Why should you use parentheses in complex conditions?</a>
  • <a class="knowledge-check-link" href="#common-patterns">How do you check if a value is within a range using and?</a>

Additional Resources

This section contains helpful links to related content. It isn't required, so consider it supplemental.

Updated on Mar 27, 2026