Boolean Values and Logic
An if statement evaluates a condition and executes a block of code only if that condition is true.
A Decision in Program Execution
An if statement gives a program a conditional decision. Python evaluates a condition and executes the associated block of code only when that condition is true. When the condition is false, Python does not execute that if-block. An optional else-block can provide alternative code for the false case.
What do you think happens?
What happens when the condition checked by an if statement is false?
Reveal answer
Answer: The if-block is skipped
An if statement executes its block only when its condition is true. If an else-block is present, that alternative block can run when the condition is false.
Following the Two Paths
The condition is the decision point. A true condition sends execution into the if-block. A false condition skips the if-block. If an else-block exists, the false path enters that alternative block instead. After the selected block finishes, execution continues with code that follows the conditional structure.
The Shape of an If Statement
An if statement has a condition followed by a colon. The colon signals to Python that a code block follows. Statements belonging to that block are indented. The else clause is optional and, when used, introduces an alternative block for the false condition.
if is_ready: start_task() else: wait_for_ready() continue_work()
Indentation Sets Block Membership
Python uses indentation to define which statements belong to a code block. Statements with the block's indentation are part of that block. When the indentation returns to the surrounding level, the block ends. Python uses indentation for this purpose instead of curly braces or keywords.
A Complete Conditional Example
Choosing Between Two Actions
Trace which action is selected when a condition is true or false.
Check the condition: The if statement evaluates condition.
Follow the true path: When condition is true, Python executes choose_first_action because that statement is inside the indented if-block.
Follow the false path: When condition is false, Python skips the if-block and executes choose_second_action in the optional else-block.
Continue: The conditional has selected one block. Code after the structure can then be reached.
The if-block handles the true condition, and the else-block handles the false condition.
Mistakes with Blocks
Leaving out the colon after the condition
The colon signals to Python that a code block follows.
Fix:
Write the condition followed by a colon: if condition:Failing to indent the if-block
Indentation defines which statements belong to the if-block.
Fix:
Indent the statement that belongs to the block.Indenting a statement that should follow the conditional structure
The indentation makes following_action part of the if-block.
Fix:
Place following_action at the surrounding indentation level when it should not belong to the if-block.Treating else as required
The else clause is optional.
Fix:
Include else only when the false condition needs an alternative block.
Practice the Decision
Write a Python if statement that checks condition, runs first_action when the condition is true, and runs second_action when the condition is false. Include the colon and indent both blocks correctly.
Hints
- Begin with if followed by the condition and a colon.
- Indent the statement belonging to the if-block.
- Put else: at the surrounding indentation level.
- Indent the statement belonging to the else-block.
Look at the following structure and identify which statements belong to the if-block: if condition: followed by an indented first_action, an indented second_action, and a non-indented following_action.
Hints
- Compare the indentation of each statement with the indentation of the other block statements.
- The block ends when indentation returns to the surrounding level.
Key Takeaways
- An if statement evaluates a condition and runs its block only when the condition is true.
- The else clause is optional and supplies an alternative block for a false condition.
- A colon at the end of the if statement signals that a code block follows.
- Indentation determines which statements belong to the if-block or else-block.
- When indentation returns to the surrounding level, the conditional block ends.
Key Takeaways
- An if statement makes execution conditional on whether a condition is true.
- The optional else-block runs as the alternative when the condition is false.
- The colon marks the beginning of the following code block.
- Indentation, rather than curly braces or keywords, identifies statements belonging to a Python block.