Conditional execution in Python is done using the if or if/else statement.
Recall that indentation is used to indicate groups of statements.
Indentation will indicate the statement or groups of statements that will be executed conditionally.
A condition is an expression that evaluates to true or false.
if statement - Generic
# Python if
Indentation is used to denote the group of statements that is executed conditionally.
Note the required colon after the condition.
if With else Statement
# Python if with else
Note the required colon after the condition and in the else.
Python if statements have the form with multiple elses have the form:
Only one of the if, elifs, or else group of statements will be executed.
There can be as many elifs as necessary.
If Statement - Example
Example, assume total and count have values:
if count > 0:
average = total / count
else: print('The average cannot be computed')