In python, if statement is useful when you want to execute the block of code/statements only when the defined condition is true.
Generally, if you want to execute the different statements based on various conditions, we will call them control flow statements. For example, showing a message either "Good Morning" or "Good Evening" based on the day's time is like executing the different statements based on the time.
In python, if…else, for loop, while loop, break, continue, and pass statements will come under the control flow statements because these will control the execution flow of programs.
In python, if
keyword is useful to define the conditional if statements. Following is the syntax of defining if
statement in python.
If you observe the above if statement syntax, the statements inside of the if condition will execute only when the boolean_expression
returns True
otherwise, the defined statements will not be executed.
In python, to create if
condition with a block of statements, you need to follow the indentation. Here, the colon (:
) symbol will indicate the start of the code block. After typing the colon (:
) symbol and click on Enter, it will increase the indentation to create the statements inside of if
statement.
To execute the multiple statements inside of if
condition, follow the same indentation for all the statements. To end if
block, reduce the indentation so that the subsequent statements will be executed as outside of if
statement.
In other programming languages, indentation is just for code readability, and curly braces { }
are used to indicate the block of statements, but in python, indentation is mandatory to indicate the block of statements.
Following is the flow chart diagram of if
statement process flow in python.
If you observe the above if
statement flow chart diagram, the block of statements will execute only when the defined condition is true otherwise if
statement will skip the execution of defined statements.
Following is the example of if
statement in python to execute the block of statements based on the defined Boolean expression.
If you observe the above example, we defined multiple if
statements with boolean expressions and followed the indentation to define the block of statements inside if
statements.
When you execute the above python program, you will get the result as shown below.
If you skip the indentation while creating if
statements, you will get build errors (IndentationError
).
Following is the example of defining if
statements in python without indentation.
If you observe the above example, we defined if statements without indentation. So, when we try to execute the above python program, we will get build errors like “excepted an indented block”.
To know more about specifying the indentation in python, refer to Python basic syntaxes.
In python, you can use logical operators such as and
, or
, not
to combine multiple conditions into a single expression in if
statement and execute the block of statements when the defined conditions are evaluated to True
or False
based on your requirements.
The logical and
operator is useful to combine the multiple conditions in python if statement. As discussed, the logical and
operator will return True
only when both operands are True
.
Following is the example of using and
operator in the boolean expression of python if statement.
If you observe the above example, we used logical and
operator to combine multiple conditions in a boolean expression of if
statement.
When you execute the above python program, you will get the result as shown below.
Like logical and
operator, the logical or
operator is also useful to combine multiple conditions in a boolean expression. The logical or
operator will return True
if either of an operand is True
.
Following is the example of using or
operator in the boolean expression of Python if statement.
If you observe the above example, we used logical or
operator to combine multiple conditions in the boolean expression of if
statement.
When you execute the above python program, you will get the result as shown below.
Same as logical and
, logical or
operators, you can use logical not
operator in python if
statement to execute the block of statements when the defined condition evaluates True
or False
based on your requirements. The logical not
operator will reverse the result if an operand is True
it will return False
and vice-versa.
Following is the example of using not
operator in the boolean expression of python if statement.
If you observe the above example, we used logical not
operator in the boolean expression of if
statement.
When you execute the above python program, you will get the result as shown below.
If you have only one line statement to execute, you can write python if
statement in a single line like as shown below.
When you execute the above python program, you will get the result as shown below.
In python, if you create if
statements inside of another if
statements, we will call it nested if
statements.
Following is the example of creating nested if statements in python.
When you execute the above python program, you will get the result as shown below.
This is how you can define if
statement in python to execute the block of statements based on the defined condition evaluation, i.e., either True
or False
.