Concepts / Combining Conditions with and and or

Combining Conditions with and and or

An if statement checks a boolean condition and executes an indented block only if that condition is true.

  • Programming

One Decision Point

Programs become useful when they can respond to different situations instead of running exactly the same code every time. An if statement creates a decision point: Python evaluates a boolean condition, runs the indented block when the condition is true, and skips that block when the condition is false. A condition that combines checks with and or or still occupies the condition position in the if header.

if conditionTrueIndented blockFalseNext statementevaluates torunevaluates toskip block
What happens after Python evaluates the condition in an if statement?

Combined Conditions

The condition is the decision-making part of an if statement. It is a boolean expression that evaluates to true or false. When a condition combines multiple checks with and or or, Python still uses the result of the whole condition to choose a path: true means that the indented block executes, while false means that Python skips the block and continues with the next statement.

Condition Atrue or falseand or orWhole conditiontrue or falseIndented blockCondition Btrue or falsecombines withcombines withproduceswhen true
How do individual boolean conditions feed into the single condition tested by if?

Reading the Header

python

This example shows the three essential parts of an if header. The word if is the keyword. The expression x > 0 is the condition. The colon marks the end of the header. The print statement is indented on the following line, so it belongs to the if block and runs only when x > 0 evaluates to true.

ifkeywordx > 0boolean condition:header endingfollowed byended by
Where are the keyword, condition, and colon located?

Tracing True and False

What do you think happens?

Suppose x has a value greater than zero. Which part of this if statement runs?

  • The indented print statement

  • Nothing in the if block

Reveal answer
Answer:

The indented print statement

The condition x > 0 is true, so Python executes the indented block immediately following the colon.

Worked example

Tracing a Condition

Determine whether the indented statement runs when x is greater than zero.

  1. 1
    Read the condition

    The condition is x > 0.

  2. 2
    Evaluate it

    For a value of x greater than zero, the condition evaluates to true.

  3. 3
    Choose the path

    Because the condition is true, Python executes the indented block.

  4. 4
    Consider the other case

    If x is not greater than zero, Python skips the indented block and continues with the next statement after it.

Result

The indented statement runs only when x is greater than zero.

The same tracing method applies when the condition contains more than one check. First identify the complete condition. Next determine whether that complete condition is true or false. Finally, select either the indented block or the next statement after the block.

Indentation Boundaries

Before
if x > 0:
    print('x is positive')
    print('inside the if block')
print('after the if block')

The first two print statements are indented and belong to the if block. The final print statement is aligned with if.

After
if x > 0:
    print('x is positive')
    print('inside the if block')
print('after the if block')

The indentation grouping is unchanged: the first two statements depend on the condition, while the final statement is outside the block.

Indentation is part of Python syntax, not merely a visual preference. Statements indented by the same amount after the header belong to the if block. A statement written at the same indentation level as the if keyword is outside that block, so it runs regardless of whether the condition was true or false.

  • Common mistake

    Forgetting to indent the statements controlled by if.

    What it looks like
    if x > 0:
    print('x is positive')
    Why it fails

    The lines that belong to the if block must be indented after the colon.

    Do this instead

    Indent the controlled statement on the following line.

  • Common mistake

    Assuming every following statement belongs to the if block.

    What it looks like
    if x > 0:
        print('inside')
    print('after')
    Why it fails

    The final statement is aligned with if, so it is outside the indented block.

    Do this instead

    Use indentation to show exactly which statements depend on the condition.

  • Common mistake

    Omitting the colon after the condition.

    What it looks like
    if x > 0
        print('x is positive')
    Why it fails

    The colon is a required part of the if header.

    Do this instead

    Place a colon after the complete condition.

  • Common mistake

    Testing the wrong boundary in a condition.

    What it looks like

    Using age > 18 when the intended rule is age >= 18.

    Why it fails

    The first condition excludes a value of exactly 18.

    Do this instead

    Write a condition that matches the rule you intend to test.

Practice Trace

EASY

Write an if statement whose condition is a boolean expression you want to check. Include the if keyword, the complete condition, and a colon. Put one or more statements in an indented block. Then trace two cases: one in which the complete condition is true and one in which it is false. For each case, state whether the indented block runs or is skipped.

Hints
  • Separate the header from the indented block.
  • Check that the header ends with a colon.
  • Evaluate the complete condition before deciding which code path runs.
  • What is the complete boolean condition?
  • Does it evaluate to true or false for the current values?
  • Which statements are indented inside the if block?
  • What is the next statement after the block if the condition is false?

Key Takeaways

  1. 1
    An if statement checks a boolean condition and chooses whether to execute its indented block.
  2. 2
    The if header contains three essential parts in order: the if keyword, a condition, and a colon.
  3. 3
    A combined condition is evaluated as one complete condition before Python chooses a code path.
  4. 4
    Indentation determines which statements belong to the if block.
  5. 5
    When the condition is false, Python skips the indented block and continues with the next statement.

At a glance

  • A combined condition still serves as the boolean decision tested by an if statement.
  • Python runs the indented block only when the complete condition is true.
  • The if header requires the if keyword, a condition, and a colon.
  • Indentation groups statements into the if block; statements aligned with if run afterward regardless of the condition.
  • Trace an if statement by evaluating the condition first and then following the true or false path.

More concepts starting with C