- 1 IF statement in Python
- 2 Python If . Syntax
- 3 Example 1: PythonIf
- 4 Example 2: Python If Statement where Boolean Expression is False
- 5 Example 3: Python If with Multiple Conditions in the Expression
- 6 Example 4: Python If with Expression evaluates to a number
- 7 Example 5: Python If with multiple statements in a block
- 8 Example 6: If nested
- 9 Summary
IF statement in Python
The Python If statement is a conditional statement where a set of statements executes based on the result of a condition.
In this Python example, we will learn about the Python If statement syntax and the different situations in which Python If command can be used.
Following is the flow diagram of the if statement in Python. Based on the evaluation of the condition, the program execution takes one of the paths.
Python If . Syntax
Following is the syntax of the if statement in Python.
if boolean_expression: statement(s)
Observe the indent provided for the statement(s) inside the if block and the colon
: after the boolean expression.
If the boolean expression returns true, the if block’s statement(s) will be executed. Otherwise, the statement(s) is not executed and program execution continues with statements after the if statement, if any.
The following is a diagram that describes the execution of the condition and the statements in the if statement.
Example 1: PythonIf
In this example, we will use a simple boolean expression created with the relational operator, less than, for the if statement condition. The statement(s) inside the if block is just a single print statement.
a = 2 b = 5 if a<b: print(a, 'is less than', b)
Run the program and you will see the following output in the console.
2 is less than 5
It is important that the condition provided in the if statement above evaluates to true, so the statement(s) inside the if block are executed.
Example 2: Python If Statement where Boolean Expression is False
In this example, we will write a if statement in which a boolean expression or condition evaluates to false.
a = 24 b = 5 if a<b: print(a, 'is less than', b)
The condition provided in the if statement evaluates to false, and therefore the statement inside the if block is not executed.
Example 3: Python If with Multiple Conditions in the Expression
When you need to write multiple conditions in a single expression, use logical operators to join them and create a compound condition. The logical operators could be: python and, python or or python not.
a = 2 b = 5 c = 4 if a<b and a<c: print(a, 'is less than', b, 'and', c)
a<b is true and
a<c that right. As a result, all conditions are true.
2 is less than 5 and 4
Example 4: Python If with Expression evaluates to a number
If the expression in the if statement evaluates to a number, then the statement(s) is executed if the number is non-zero 0. 0 is considered false and non-zero (positive or negative) is considered true.
In this example, we’ll write a Python If statement, where the boolean expression evaluates to a number.
a = 2 if a: print(a, 'is not zero')
Since the value of the evaluated condition is
2is a non-zero number, the statement(s) inside the if block are executed.
2 is not zero.
Example 5: Python If with multiple statements in a block
In the syntax section, we mentioned that there can be multiple statements inside if block.
In this example we will write three statements inside if block.
a = 2 if a: print(a, 'is not zero') print('And this is another statement') print('Yet another statement')
We just write the print statements in this example inside the if block. But you can write a set of any valid python statement(s) inside the if block.
Please note that if-block statements have the same indented in.
2 is not zero And this is another statement Yet another statement
Example 6: If nested
You can write a Python If statement inside another Python If statement. This is called nesting.
In this example, we will write a nested if block: an if block inside another if block.
a = 2 if a!=0: print(a, 'is not zero.') if a>0: print(a, 'is positive.') if a<0: print(a, 'is negative.')
As the first/external condition
a!=0 evaluates to true, execution goes into the if block. And the if statements inside this outer if block are treated as another Python statement and executed accordingly.
2 is not zero. 2 is positive.
In this guide of Python example we have learned what is If statement in Python, what is it used for, how to write nested if, if statements, all with the help of some detailed examples.
Hope this helps!