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
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()- 1Python evaluates
is_ready. - 2If the condition is true, Python executes
start_task. - 3If the condition is false, Python executes
wait_for_readyinstead. - 4After the selected block, Python reaches
continue_work.
- The if keyword begins the conditional statement.
is_readyis the condition being evaluated.- The colon signals that the if-block follows.
- The indented
start_taskstatement belongs to the if-block. - The else clause introduces the alternative block.
- The indented
wait_for_readystatement belongs to the else-block. continue_workis not indented under either block, so it follows the conditional structure.
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.
if condition:
first_action()
second_action()
third_action()first_action and second_action are indented under the if statement; third_action is outside the block.
if condition:
first_action()
third_action()Only first_action is indented under the if statement; third_action remains outside the block.
A Complete Conditional Example
Choosing Between Two Actions
Trace which action is selected when a condition is true or false.
- 1Check the condition
The if statement evaluates condition.
- 2Follow the true path
When condition is true, Python executes
choose_first_actionbecause that statement is inside the indented if-block. - 3Follow the false path
When condition is false, Python skips the if-block and executes
choose_second_actionin the optional else-block. - 4Continue
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
Common mistake Leaving out the colon after the condition
What it looks likeif condition action()Why it failsThe colon signals to Python that a code block follows.
Do this insteadWrite the condition followed by a colon: if condition:
Common mistake Failing to indent the if-block
What it looks likeif condition: action()Why it failsIndentation defines which statements belong to the if-block.
Do this insteadIndent the statement that belongs to the block.
Common mistake Indenting a statement that should follow the conditional structure
What it looks likeif condition: action() following_action()Why it failsThe indentation makes
following_actionpart of the if-block.Do this insteadPlace following_action at the surrounding indentation level when it should not belong to the if-block.
Common mistake Treating else as required
Why it failsThe else clause is optional.
Do this insteadInclude 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
- 1An if statement evaluates a condition and runs its block only when the condition is true.
- 2The else clause is optional and supplies an alternative block for a false condition.
- 3A colon at the end of the if statement signals that a code block follows.
- 4Indentation determines which statements belong to the if-block or else-block.
- 5When indentation returns to the surrounding level, the conditional block ends.
At a glance
- 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.