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
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.
Read the condition: The condition is x > 0.
Evaluate it: For a value of x greater than zero, the condition evaluates to true.
Choose the path: Because the condition is true, Python executes the indented block.
Consider 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
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.
Forgetting to indent the statements controlled by if.
The lines that belong to the if block must be indented after the colon.
Fix:
Indent the controlled statement on the following line.Assuming every following statement belongs to the if block.
The final statement is aligned with if, so it is outside the indented block.
Fix:
Use indentation to show exactly which statements depend on the condition.Omitting the colon after the condition.
The colon is a required part of the if header.
Fix:
Place a colon after the complete condition.Testing the wrong boundary in a condition.
The first condition excludes a value of exactly 18.
Fix:
Write 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
- An if statement checks a boolean condition and chooses whether to execute its indented block.
- The if header contains three essential parts in order: the if keyword, a condition, and a colon.
- A combined condition is evaluated as one complete condition before Python chooses a code path.
- Indentation determines which statements belong to the if block.
- When the condition is false, Python skips the indented block and continues with the next statement.
Key Takeaways
- 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.