THE IF ELIF STATEMENTS IN PYTHON
We are now quite familiar with the Python if else statements. We have some expression, and if the expression evaluates to True, then the if block executes, and if the expression evaluates to False, then the else block executes. So here, in the else, we are not checking any condition, but at times, it would be a requirement, to check some condition here as well, and in such situations, we can have the elif keyword.
Python if elif statements
Let’s understand this thing with an example, where we are going to take a number as user input, and then we will check whether the number entered by the user, is 0, greater than 0, or less than 0. Let’s have a look at the simple program.
As you can see in the above program, we are taking a number as user input, and then we are checking first of all, that whether the number is greater than zero. If the expression evaluates to False, then we are again checking that if the number is less than zero, and if the expression again evaluates to False, then we are printing that the number is zero! Let’s have a look at the output of the above program.
Please enter a number: -29
The number is less than zero!
As you can see, we tried to input some negative number, making the expression after the first if statement as False, and we moved to the elif statement, where we are again checking the condition, for number to be less than zero. This time, the expression evaluates to True, due to which, we get the output as the number is less than zero! This is simply helpful for us, at times, when we need to check some condition again, when the expression with if evaluates to False.
Let’s consider one more example, so that we become more familiar with the concept. Note that with the elif keyword, we are able to again check some other condition, when the expression from the previous if evaluated to False.
In the below example, we are going to check if the shopkeeper is in profit, or loss, or no profit no loss. For this, we would simply take cost price, and selling price as user inputs. The simple logic behind this is that if the selling price is more than the cost price, then it is profit, and if the selling price is less than the cost price, then it is loss. Let’s have a look at the program –
As you can see in the above program, we are taking the cost price and the selling price as the user input. Now, first of all we are checking that if the selling_price is greater than the cost_price, and if this evaluates to True, we simply print that the person is in profit, with the amount. Now, if the expression written with the if statement evaluates to False, we are going into the elif, and again checking another condition, that if the cost_price is greater than the selling_price, which is the loss condition. If this condition is also False, then we are printing that there is no profit no loss. Let’s have a look at the output –
Please enter the cost price:1200
Please enter the selling price:1850
You are in profit! amount = 650.0
As you can see from the output, we enter the cost price, and the selling price, and just get the output accordingly. So, in the situations, when we are required to check some other condition, if the previous condition from the if was False, we can simply make use of the if elif statements.