Summary: in this tutorial, you’ll learn how to use the Python if statement to execute a block of code based on a condition.
The simple Python if statement #
You use the if statement to execute a block of code based on a specified condition.
The syntax of the if statement is as follows:
if condition:
if-blockCode language: Python (python)The if statement checks the condition first.
If the condition evaluates to True, it executes the statements in the if-block. Otherwise, it ignores the statements.
Note that the colon (:) that follows the condition is very important. If you forget it, you’ll get a syntax error.
The following flowchart illustrates the if statement: