No history yet

Control Flow Logic

Decisions with Multiple Conditions

You already know how to use if and else to make your code follow a specific path. But real-world problems often have more than two simple options. Logic rarely boils down to a single question. Instead, it involves checking several conditions at once.

Python handles this using Boolean logic operators: and, or, and not. These let you build more complex questions into your conditional statements.

OperatorWhat it doesExample
andReturns True only if both conditions are true.age > 18 and has_license == True
orReturns True if at least one condition is true.is_student or is_senior
notInverts the result; turns True to False and vice versa.not is_logged_in

Let's say a streaming service offers a discount to users who are either under 25 or are students. We can check this with a single if statement.

age = 22
is_student = False

if age < 25 or is_student:
    print("Discount applied!")
else:
    print("Standard pricing.")

# Output: Discount applied!

Layering Your Logic

Sometimes, one decision depends on the outcome of another. You might ask one question, and if the answer is yes, you then ask a follow-up question. This is handled with nested if statements.

Imagine a system for approving online comments. The first check might be whether the user is logged in. If they are, a second check determines if their account is in good standing.

Lesson image

Here's how that logic looks in code. The inner if/else block only runs if the first condition (is_logged_in) is true.

is_logged_in = True
account_status = "good"

if is_logged_in:
    print("User is logged in. Checking account status...")
    # This is the nested block
    if account_status == "good":
        print("Comment approved.")
    else:
        print("Account suspended. Comment held for review.")
else:
    print("User must be logged in to comment.")

Challenge: An Eligibility System

Let's build a system to check if an applicant is eligible for a specific program. This will combine everything we've covered. The rules are:

  1. The applicant must be 18 or older.
  2. They must reside in either the "USA" or "Canada".
  3. If they are in the "USA", their status must be "Active".
  4. If they are in the "Canada", their status doesn't matter.

How would you structure this? The nesting might look something like this:

Here is one way to write the code. Notice how the logic for the country and status is nested inside the age check.

age = 30
country = "USA"
status = "Active"

if age >= 18:
    if country == "USA":
        if status == "Active":
            print("Applicant is eligible.")
        else:
            print("Ineligible: Must have Active status in the USA.")
    elif country == "Canada":
        print("Applicant is eligible.")
    else:
        print("Ineligible: Must be in the USA or Canada.")
else:
    print("Ineligible: Applicant is too young.")

While nested if statements work, they can sometimes become hard to read. You can often simplify complex logic by combining conditions.

The eligibility rules can be expressed as a single, more complex condition: an applicant is eligible if they are 18 or older AND (they are from the USA with active status OR they are from Canada).

This also reveals an interesting Python behavior called short-circuit evaluation. When evaluating an and condition, if the first part is False, Python doesn't even bother checking the second part. Why? Because the entire expression can't possibly be True. The same happens with or—if the first part is True, the whole thing must be True, so the second part is skipped. This can make your code slightly faster.

# A flattened version of the same logic

if age >= 18 and ((country == "USA" and status == "Active") or (country == "Canada")):
    print("Applicant is eligible.")
else:
    print("Applicant is ineligible.")

Mastering logical combinations and nesting allows you to translate almost any set of real-world rules into a program that makes intelligent decisions.

Quiz Questions 1/4

What will be the output of the following Python code snippet?

Quiz Questions 2/4

In Python, when does short-circuit evaluation happen with an and operator?