Python If Statement

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 it 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.

If Statement Syntax

In python, if keyword is useful to define the conditional if statements. Following is the syntax of defining if statement in python.

 

if boolean_expression:

    statement1

    statement2

    ....

    statementN

If you observe the above if statement syntax, the statements inside of 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.

Python If Statement Flow Chart

Following is the flow chart diagram of if statement process flow in python.

 

Python if statement flow chart diagram

 

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.

If Statement Example

Following is the example of if statement in python to execute the block of statements based on the defined Boolean expression.

 

x = 10

y = 20

if x > y:

    print("x is greater than y")

if y > x:

    print("y is greater than x")

print("x value:", x)

print("y value:", y) 

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 like as shown below.

 

y is greater than x

x value: 10

y value: 20

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.

 

x = 10

y = 20

if x > y:

print("x is greater than y")

if y > x:

print("y is greater than x")

print("x value:", x)

print("y value:", y) 

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 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.

Python If And

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.

 

x = 30

y = 20

if x > y and x == 30:

    print(" x value is 30 and x is greater than y")

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 like as shown below.

 

x value is 30 and x is greater than y

Python If Or

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.

 

x = 30

y = 20

if x > y or x == 10:

    print(" x equals to 10 or x is greater than y")

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 like as shown below.

 

x equals to 10 or x is greater than y

Python If Not

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 boolean expression of python if statement.

 

x = False

y = 20

if not x:

    print("x is not True")

if not y == 30:

    print("y is not equals to 20")

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 like as shown below.

 

x is not True

y is not equals to 20

One Line If Statement

If you have only one line statement to execute, you can write python if statement in a single line like as shown below.

 

x = 30

if x > 10: print("x is greater than 10")

When you execute the above python program, you will get the result like as shown below.

 

x is greater than 10

Python Nested If

In python, if you create if statements inside of another if statements, we will call it as nested if statements.

 

Following is the example of creating nested if statements in python.

 

x = 30

if x > 10:

    print("x is greater than 10")

    if x < 40:

        print("x is less than 40")

print("x value:", x)

When you execute the above python program, you will get the result like as shown below.

 

x is greater than 10

x is less than 40

x value: 30

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