Python if elif else statement by Manish Sharma

In this tutorial you’ll see the comparison between a nested if-else statement and elif Python statement. So get ready to learn something new!

We learnt that Python ‘if statement’ is a single alternative decision structure in the last tutorial. And if-else is a dual alternative conditional controlled decision structure. Both these statements have some advantages however at the same time they have some shortcomings too. I will show you how to overcome the shortcomings of both these statements using elif python statement in this tutorial.

Major Shortcomings of Python If statement.

Firstly, what are the major shortcomings of Python if and if-else statement?
With ‘Python If’  we only get a single alternative path of execution for decision making. Whereas if-else gives us two alternative paths of execution. But what if we need a multiple alternative decision structure?

‘else’ is not conditional controlled

Though the else statement gives us an extra alternative path of execution that is not conditional controlled. An else statement contains a block of code. It executes when the condition/expression of the if statement evaluates to false. 

Meaning the body of the else statement is going to run every time the corresponding if statement gets evaluated to false, no matter what. We cannot control it. 

Elif Python Statement – Solution to All The Shortcomings.

If you are wondering how to overcome these shortcomings.

All these problems can easily be solved using elif Python Statement. 

elif is an acronym of else if. It is a multiple alternative conditional controlled decision structure. 

elif Python Statement is not only the solution of the above-stated problems. But it is also an easy to read, less complex and a simplified version of Python Nested If-Else structure.

The Syntax of elif Python Statement.

if condition :
  Block of statements.
elif condition :
  Block of statements.
elif condition :
  Block of statements.
. . .
else :
  Block of statements.

The syntax is pretty much the same as the Python if statement except for the elif part. You can insert as many elif clauses as you want in your code. 

Examples of Elif Python Statement.

I said earlier in this tutorial that elif Python statement is a simplified version of Nested if-else statement of Python Programming. Now I will explain to you what does that mean with the help of a practical example. 

Suppose your university or school asked you to develop an application. One of the modules of that application requires you to tell the student their division of passing, based on their marks.

You are also provided with some initial criteria, using which you must design this module. That criteria is –

python if elif else example.png

Student with marks over 50 and less than 60 has passed with the second division. Student with marks over 60 and less than 75 has passed with the first division. Whereas the student with marks over 75 and less than 90 has passed with the first division with distinction. And if a student has got marks over 90 then he or she has passed with an outstanding performance.

Example 1: Nested if-else

Let me first show you how to use these four conditions to make a decision using nested if-else statement. 

marks_obt = float(input('Enter Total Marks You Have Secured: '))
if marks_obt >=50 and marks_obt <60:
    print('You Have Got SECOND division', end='\n\n')
else:
    if marks_obt >=60 and marks_obt <75:
        print('You Have Passed With FIRST division', end='\n\n')
    else:
        if marks_obt >=75 and marks_obt <90:
            print('You Have Got FIRST division with DISTINCTION', end='\n\n')
        else:
            if marks_obt >90:
                print('Congratulation! OUTSTANDING PERFORMANCE', end='\n\n')
            else:
                print("Sorry! You Have FAILED!", end='\n\n')
print('Good Luck For Your Future')

We are writing a Python if clause and an else clause in this program. Furthermore, inside that else clause we are again writing another if and else clause. And we are repeating this process. 

Though this program is producing the desired result yet not in an efficient way. Don’t you think that this Python program is already very confusing? What if we need to add four more conditions? Then it will become an ‘out of control’ mess.

Example 2: elif Python statement

Now let’s try to achieve the same result as the one produced by the earlier code. But in a much simpler way by using elif python statement. 

marks_obt = float(input('Enter Total Marks You Have Secured: '))
if marks_obt >=50 and marks_obt <60:
    print('You Have Got SECOND division', end='\n\n')
elif  marks_obt >=60 and marks_obt <75:
    print('You Have Passed With FIRST division', end='\n\n')
elif marks_obt >=75 and marks_obt <90:
    print('You Have Got FIRST division with DISTINCTION', end='\n\n')
elif marks_obt >90:
    print('Congratulation! OUTSTANDING PERFORMANCE', end='\n\n')
else:
    print("Sorry! You Have FAILED!", end='\n\n')

print('Good Luck For Your Future')

First thing you will notice about this code is that – it is much better-looking. As compared to the earlier one. This code of elif python statement will produce the same result as the one produced by the earlier code. However in a more efficient way. 

What happens in the background?

Upon the execution of this if-elif-else statement the first thing that the Python interpreter will do is to check the expression of If Clause. In case it is proved to be true then the block of statements that follow the if clause will be executed by the interpreter. And the rest of the if-elif-else structure will be subsequently ignored. However, if that condition is evaluated to be false then, the interpreter will jump on to the next elif clause. And evaluate its condition. If that’s found out to be true then the immediate block of statements will be executed. Additionally, the rest of the structure will then be ignored.

Furthermore, this process will keep on going. Until a condition is found to be true or for that matter there are none remaining elif clauses. Consequently, if no condition is found to be true then the block of statements that follow the else clause will be executed.  

That’s what happens in the background when you run an if-elif-else statement of Python Programming.

I suggest you to read the previous blog for precautions that you must follow while writing a Python If-elif-else statement. You can also click here to watch the video for detailed knowledge. 

If you have any other queries then do leave a comment on my Facebook Page. Thanks & have a great day!