Combining Conditions with and and or
An if statement checks a boolean condition and executes an indented block only if that condition is true.
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.
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.
Reading the Header
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.
Tracing True and False
What do you think happens?
Suppose x has a value greater than zero. Which part of this if statement runs?
Reveal answer
The indented print statement
The condition x > 0 is true, so Python executes the indented block immediately following the colon.
Tracing a Condition
Determine whether the indented statement runs when x is greater than zero.
- 1Read the condition
The condition is x > 0.
- 2Evaluate it
For a value of x greater than zero, the condition evaluates to true.
- 3Choose the path
Because the condition is true, Python executes the indented block.
- 4Consider the other case
If x is not greater than zero, Python skips the indented block and continues with the next statement after it.
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
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.
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 likeif x > 0: print('x is positive')Why it failsThe lines that belong to the if block must be indented after the colon.
Do this insteadIndent the controlled statement on the following line.
Common mistake Assuming every following statement belongs to the if block.
What it looks likeif x > 0: print('inside') print('after')Why it failsThe final statement is aligned with if, so it is outside the indented block.
Do this insteadUse indentation to show exactly which statements depend on the condition.
Common mistake Omitting the colon after the condition.
What it looks likeif x > 0 print('x is positive')Why it failsThe colon is a required part of the if header.
Do this insteadPlace a colon after the complete condition.
Common mistake Testing the wrong boundary in a condition.
What it looks likeUsing age > 18 when the intended rule is age >= 18.
Why it failsThe first condition excludes a value of exactly 18.
Do this insteadWrite a condition that matches the rule you intend to test.
Practice Trace
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
- 1An if statement checks a boolean condition and chooses whether to execute its indented block.
- 2The if header contains three essential parts in order: the if keyword, a condition, and a colon.
- 3A combined condition is evaluated as one complete condition before Python chooses a code path.
- 4Indentation determines which statements belong to the if block.
- 5When 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.