If, If Else In Python, Elif, Nested If | Decision-Making Statements in Python

In this article, we will quickly look at some important decision making statements in Python like if statement in Python, if else in Python, elif in Python and Nested if statement.

What Are Decision Making Statements?

Decision-making statements are those that help in deciding the flow of the program. For example, at times, you might want to execute a block of code only if a particular condition is satisfied. Well, in this case, decision-making statement will be of great help. Let us consider an example to understand this better.

Suppose you are given a number and are asked to check if it is an even number or not. How would you do it?

The first thought that would pop up your mind is this – “If the number is divisible by 2, then it is an even number, else it is an odd number”. That’s absolutely the right logic. But when this has to be coded, you will need the help of decision-making statements. To understand this, let’s now look at how they function.

 

a) if statement

This is the simplest decision-making statement in Python. It is used to decide if a particular block of code needs to be executed or not. Whenever the given condition is True, then the block of code inside it gets executed, else it does not.

The if statement flowchart:

if statement in python

The syntax for if statement:

An if statement should be preceded by the keyword if and ended with a colon :. The condition of an if statement can be written with or without round brackets.

if condition:
body of if

Example of if statement:

#program to check if num1 is less than num 2
num1, num2 = 5, 6
if(num1 < num2):
    print("num1 is less than num2")
Output: num1 is less than num2

Explanation: Here the value 5 is less than 6, so this means the condition is True. Hence the print statement inside the body of if gets executed.

Shortcut for if statement (Short Hand if or one line if)

If you have only one statement to execute, then you can put it on the same line as the if statement. Let’s try doing this for the above example.

num1, num2 = 5, 6
if(num1 < num2): print("num1 is less than num2")

The Difference….

Now, try to execute the below-given code and observe the difference.

#program to check if num1 is less than num 2
num1, num2 = 6, 5
if (num1 < num2):
    print("num1 is less than num2")

Did your Python IDE print anything? No, it will not.

In the above-given code, the value 6 is greater than 5, this means the condition is False. We have instructed the interpreter to print only when the condition is True and hence it in this case, nothing gets printed. However, you can also instruct the interpreter what to do when the condition is False and this can be done using else statement.

 

b) if else statement

The statements written within the else block get executed whenever the if condition is False. You can imagine the flow of execution this way,

The if else statement flowchart:

if else in python

The syntax for if else statement:

Else statement is preceded by the keyword ‘else’ and it also ends with a colon :. Unlike the if, the else statement will not have any conditional statements.

if condition:
body of if
else:
body of else

if else in Python Examples:

#program to check if a num1 is less than num2
num1, num2 = 6, 5
if (num1 < num2):
     print("num1 is less than num2")
else:
     print("num2 is less than num1")
Output: num2 is lesser than num1

Explanation: In the above-given code, the if condition is False and hence the control shifts to the else block. Hence the statement written within the else block gets printed.

Shortcut of if else (Short Hand if … else or One line if else)

If you have only one statement each for if and else, then they can be put in the same line. This can be done as shown below

num1, num2 = 6, 5
print("num1 is less than num2") if (num1 < num2) else print("num2 is less than num1")
Output: num2 is less than num1

The difference….

Now, let us try to execute the same above discussed code with different input values.

#program to check if num1 is less than num2
num1, num2 = 5, 5
if (num1 < num2):
print("num1 is less than num2")
else:
print("num2 is less than num1")
Output: num2 is lesser than num1
Why was the output wrong?

Since both the input values are equal, the condition num1<num2 is False. So immediately the control shifts to else block and the print statement inside it gets executed. However, this is not logically correct as both the input values are equal (5 = 5).

How to rectify this?

Your program should logically include every possible test case and only then your program will produce accurate results. So this mean you need to add another conditional statement to check if num1 is equal to num 2. Let us now see how this can be done.

 

c) elif statement

The keyword elif is a combination of else and if. This statement works similar to ‘else if’ statements in the C and other languages. The statements in elif block get executed when the previous if condition is False.

The elif statement flowchart:

elif statement in python

The syntax for elif statement:

if condition:
body of if
elif condition:
body of elif
else:
body of else

Examples of elif statements:

Let us continue with our previous example where the inputs were the same.

num1, num2 = 5, 5
if(num1 > num2):
    print("num1 is greater than num2")
elif(num1 == num2):
    print("num1 is equal to num2")
else:
    print("num1 is less than num2")
Output: num1 is equal to num2

Explanation: Since both the values are equal, the if condition is False. Now the control shifts to elif statement and here the condition is True. Hence the statements in the elif block get executed.

 

d) Nested if statements

One if statement inside another if statement is called Nested if statement. The structure and flow of execution can be assumed as shown below.

nested if in python

The syntax of nested if statement:

if condition1:
    statements
    if condition2:
        statements
    else:
        statements
else:
    statements

Examples of nested if statements:

Let us now try to write a program to check if a given number is positive or not.

num1 = 5
if (num1 != 0):
     if(num1 > 0):
          print("num1 is a positive number")
     else:
          print("num1 is a negative number") 
else:
     print("num1 is neither positive nor negative")
Output: num1 is a positive number

Explanation: Here, the first if condition is True. Hence the interpreter checks for the inner if condition. This is also True. Hence, the statements inside inner if get executed. 

Consider the same code with negative value as the input. 

num1 = -6
if (num1 != 0):
     if(num1 > 0):
          print("num1 is a positive number")
     else:
          print("num1 is a negative number") 
else:
     print("num1 is neither positive nor negative")
Output: num1 is a negative number
Explanation: Here, the first if condition is True and the inner if condition is False. Hence the statement in the inner else block gets executed.

Important Points to Note About Decision Making Statements

  • Indentation is very important for a Python code to execute. Based on indentation, the Python interpreter identifies a particular block of code. If a piece of code is not indented, the compiler will throw an error.
  • You can use logical operators like AND, OR to combine your conditional statements used in if or else.
  • Body of if cannot be empty. But if you for some reason want it to be empty, then use the pass statement to avoid getting errors. You can use the pass statement this way,
num1, num2 = 5, 6
if (num1 > num2):
    pass