Python Programming - if, else and elif

Sohail
Sohail

Table of Contents

​If you have the time, you can read this article else you can do something else. Sounds logical, doesn’t it? Well, That’s programming for you in a nutshell. Programming is all about writing code that will give different outputs depending upon the state of various variables at the moment.

Follow the full series – Learn Python

The IF Else Clause

The IF Else clause is very useful when it comes to making changes in program execution. The syntax for an if-else clause is as follows.

if(condition):
Execute A
else:
    Execute B

The syntax is very much human readable, You can see that the first 2 says if(condition): Execute A. This means that if the condition returns true, execute A. The next 2 lines say that if the above condition was not true and A was not executed, execute B. A and 2 can be any N number of code statements. ​

A Simple Example

For now, just recreate the following code in your local python file.

python if-else statement

The code looks very self-explanatory. We have declared 2 variables, X with a value of 5 and Y with a value of 2. Next, we have written an if-else block where we check for a simple Condition. If X is more than y, it will print so else the other statement. The code was written after if and else blocks need a tab’s space so as to demonstrate that this code falls in a particular block. So we basically have 2 blocks of code. The if block and the else block.​We know for a fact without even running it, that our value of X is indeed more than Y, so it should print X is more than Y on the console. Let us now have a look at the output.

run python script in terminal

And our code does exactly what we wanted it to. Try changing the value of variables and conditional operators to better understand the concept of conditions.

The elif keyword

We just saw how we can use if and else to run different blocks code based on various conditions. So far we have seen the if and the else blocks. But what if we wanted to test for multiple conditions. In that case, we would have to use the elif keyword. Actions often speak louder than words, Hence recreate the following code in your systems.

elif keyword python

The code is quite similar to the one we wrote earlier with a few changes. You can see that the values of both variables are now equal. Also, we have a new block called the elif block. Python will check for the condition in the if block if it is true, the corresponding code will be executed. If the condition from the if clause is not true, Python will check for the condition in the next i.e elif clause. If the condition is true, the code from that block will be executed. Else the code form the else block will be executed.

​This was a simple example, However, in complex programs, there might be multiple conditions. The beauty about the elif clause is that you can have just one if clause and one corresponding else clause but there can be as many elif clauses as you want. ​

python3 conditional statement

If you’ve followed everything up until now you should get an output similar to the one above.

Conclusion

We saw a few programs that used if, else and elif clauses. It is important to note that at times, one may use just an if clause alone however an else or elif clause should always follow an existing if clause. I am sure you have a decent understanding of how if else and elif clauses can be used to make dynamic programs. If you face any difficulty, let me know in the comment section below.

Learn PythonUncategorizedtutorial