In this article, we will understand the need of if-statement in python? What’s the syntax of if-statement in python? Then we will look at various examples of if-statement like using if statement with multiple conditions in python and many more.

In python, by default, statements are executed in sequential order, i.e., one after another. For example, we have these four lines of statements,

x = 18                              # Statement 1
print('Value of x is: ', x)         # Statement 2
print('x is less than 10')          # Statement 3
print('x is a single digit number') # Statement 4
print('This is last line')          # Statement 5

Now if we run the above code, all these statements will execute one after another and output will be like,

Value of x is:  18
x is less than 10
x is a single digit number
This is last line

But while writing applications, we generally don’t want it to run all the lines or statements sequentially. We want our code to do some decision making and then execute specific statements at the correct time, i.e., based on those logical decisions. Like in the above code, we want to run statement 3 & statement 4 only if x is smaller than 10.

So, our flow of code should be like this,After executing the first two statements one after another, in the 3rd line, there is an if statement with a condition, i.e., is x smaller than ten or not? If yes, then only execute the next two statements otherwise skip them and jump directly to the last statement.
We can make such kind of decision making and change the flow of code using an if statement.

Syntax of if-statement in python

if condition-expression:
    statement_1
    statement_2
    statement_3

In python, we don’t have the concept of brackets to mark the start and end of a block. Instead, it uses the indentation to represent a block. Like here, after the “if statement” there are two other statements with one level of indent. It denotes the block area of the “if statement”. This collection of statements in a block is also called a suite in python.

“if” keyword is always be followed by a conditional expression, which should evaluate to a bool value, i.e., either True or False. If the condition evaluates to True, then the interpreter executes the statements in “if” suite, i.e., the code statements in the if-block. Whereas if the condition evaluates to False, then the interpreter skips the lines in the if-block and jumps directly to the end of if-block.

Examples of if-statement in python

Let’s take an example of the statements which we used earlier,

x = 18
print('Value of x is: ', x)
if x < 10:
    print('x is less than 10')
    print('x is a single digit number')
print('This is last line')

Output:

Value of x is:  18
This is last line

Here first two lines were executed in sequential order, and after that, the interpreter encountered an if statement. Then it evaluated the conditional expression in the if-statement and as x was 18 (smaller than 10), so conditional expression was considered to False. So, the interpreter skipped the if-block suite, i.e., the lines in the if block and directly jumped to the end of if-block and executed the statements after the if-block in sequential order.

So, the flow of our code was like this,

Another Example of if-statement in python

Now let’s take another example,

x = 5
print('Value of x is: ', x)
if x < 10:
    print('x is less than 10')
    print('x is a single digit number')
print('This is last line')

Output:

Value of x is:  5
x is less than 10
x is a single digit number
This is last line

Here first two lines were executed in sequential order, and after that, the interpreter encountered an if statement. Then it evaluated the conditional expression in the if-statement, and as x was 5 (smaller than 10), therefore conditional expression evaluated to True. So, the interpreter ran the lines in the if-block suite, i.e., the lines in the “if block”. Then statements after the “if block” was executed in sequential order.

So, the flow of our code was like this,

Python: if-statement with multiple conditions

In all the above examples, we provide a single condition in with the if-statement, but we can give multiple conditions too.
For example, let’s enhance the previous example to check if x is greater than ten but less than 20 and its value should not be 15,

x = 12
print('Value of x is: ', x)
if x > 10 and x < 20 and x != 15:
    print('x is greater than 10')
    print('x is smaller than 20')
    print('x is not 15')
print('This is last line')

Output:

Value of x is:  12
x is greater than 10
x is smaller than 20
x is not 15
This is last line

Here if-statement is checking multiple conditions, and if all the conditions satisfy and evaluate to True, then the interpreter executes the code in the if-block.

Like in the above example, x was 12, so it was greater than 10 and lesser than 20, and also it was not 15. So, in if statement, all the conditions evaluated to True. So condition expression became,

True and True and True

This eventually translated to True. Due to which code in if block got executed.

Let’s look at a negative example,

x = 15
print('Value of x is: ', x)
if x > 10 and x < 20 and x != 15:
    print('x is greater than 10')
    print('x is smaller than 20')
    print('x is not 15')
print('This is last line')

Output:

Value of x is:  15
This is last line

Here if the statement is checking multiple conditions but the complete condition expression evaluated to False, therefore code in the if-block was not executed, i.e., interpreter skipped the statements in if block.

It happened because in the above example x was 15, so it was greater than 10 and lesser than 20 and but it was equal to 15. So, in if statement, all the conditions did not evaluate to True. So condition expression became,

True and True and False

This eventually translated to False and due to which suite of if-statement, i.e., code in if-block, was not executed. Interpreter just skipped the lines in that if-block and jumped directly to the statement after the if block.

Conclusion

We can use if-statement to control the flow of our code based on conditions on different values.

Join a list of 2000+ Programmers for latest Tips & Tutorials