Python If – Syntax & Examples

Python IF Statement

Python If statement is a conditional statement wherein a set of statements execute based on the result of a condition.

In this Python example, we will learn about Python If statement syntax and different scenarios where Python If statement can be used.

Following is a flow diagram of Python if statement. Based on the evaluation of condition, program execution takes one of the paths.

Python If

Syntax of Python If

Following is the syntax of if-statement in Python.

if boolean_expression:
    statement(s)

Observe the indentation provided for statement(s) inside if block and the colon : after boolean expression.

If the boolean expression returns true, the statement(s) of the if block are executed. Else, the statement(s) are not executed, and the program execution continues with the statements after if statement, if there are any.

Following is a flowchart depicting the execution of condition and statements in the if statement.

Python If

Example 1: Python If

In this example, we will use a simple boolean expression formed with relational operator, less than, for the if statement condition. The statements(s) inside the if block is just a single print statement.

Python Program

a = 2
b = 5

if a<b:
	print(a, 'is less than', b)
Run this program

Output

Run the program, and you will see the following output in console.

2 is less than 5

It is trivial that the condition provided in the above if statement evaluates to true, therefore the statement(s) inside the if block is executed.

Example 2: Python If Statement where Boolean Expression is False

In this example, we will write an if statement where the boolean expression or condition evaluates to false.

Python Program

a = 24
b = 5

if a<b:
	print(a, 'is less than', b)
Run this program

Output

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.

Python Program

a = 2
b = 5
c = 4

if a<b and a<c:
	print(a, 'is less than', b, 'and', c)
Run this program

Output

a<b is true and a<c is true. As a result the whole condition is true.

2 is less than 5 and 4

Example 4: Python If with Expression evaluating to a Number

If the expression in the if statement evaluates to a number, then the statement(s) are executed if the number is non-zero. zero is considered to be false and non-zero (positive or negative) is considered true.

In this example, we will write a Python If statement, where the boolean expression evaluates to a number.

Python Program

a = 2

if a:
	print(a, 'is not zero')
Run this program

Output

As the value of condition evaluates to 2, which is a non-zero number, the statement(s) inside if block are executed.

2 is not zero.

Example 5: Python If with multiple statements in the block

In the syntax section, we already mentioned that there can be multiple statements inside if block.

In this example, we will write three statements inside if block.

Python Program

a = 2

if a:
	print(a, 'is not zero')
	print('And this is another statement')
	print('Yet another statement')
Run this program

We have written only print statements in this example inside if block. But you may write a set of any valid python statement(s) inside the if block.

Please note that the statements of if-block have same indentation.

Output

2 is not zero
And this is another statement
Yet another statement

Example 6: Nested If

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.

Python Program

a = 2

if a!=0:
	print(a, 'is not zero.')
	if a>0:
		print(a, 'is positive.')
	if a<0:
		print(a, 'is negative.')
Run this program

Output

As the first/outer condition a!=0 evaluates to true, the execution enters the if block. And the if statements inside this outer if block are considered as yet another Python statements and executed accordingly.

2 is not zero.
2 is positive.

Summary

In this tutorial of Python Examples, we learned what an If statement is in Python, what is it used for, how to write an if statement, nested if, all with the help of some well detailed examples.