Using elif statement Part 2

Lesson Details:
June 29, 2020


I: Introduction

Python is a high-level, general-purpose programming language which can be used for a wide range of applications. It has a simple syntax, making it easy to understand and learn, and a very clear distinction between statements and expressions enables both human readability and machine parsing.

Python is free, open source, and has full functionality. Programmers are able to use Python anywhere, on any platform of their choice, for any purpose.

Python is one of the fastest growing languages worldwide, with many websites using this language for their backend. The number of packages that can be installed using Python’s package manager ‘pip’ is enormous, making it extremely flexible. This also makes it easy to install Python libraries.

While some people associate Python with web development only, the fact is that Python is used in every field including finance (Bloomberg), science (NASA), education (Khan Academy), game development (Minecraft), music (Spotify), robotics (Roomba), art (3D Cad), and many others.

II: Body

A: Using elif statement part 2

This is the second part of the series on using the ‘elif’ statement in Python programming. We had discussed this in an earlier tutorial. For more details, please refer to the below link: https://www.codexpert.com/blog/using-elif-statement-in-python-programming/. Here we will discuss how to use ‘if’, ‘elif’ and ‘else’ statements in python programming examples.

The else statement is used in conjunction with an if or elif statement. If all the conditions in the if block are satisfied, then the statements in the then block are executed. Else, if all the conditions in the elif block are satisfied, then the statements in the then block are executed. Finally, if none of the conditions are satisfied, then the statements in the else block are executed.

Let us consider an example where we have two variables x and y, each containing values 0 and 1 respectively, and we want to know whether x = y or not? The code snippet is shown below:

x = 0 y = 1 else: print("x is not equal to y") if x == y: print("x equals y") elif x != y: print("x is not equal to y") else: print("x is equal to y")

The above code snippet will output “x equals y”. If you are wondering why this happened, then let me give you an intuition behind this. The program first checks whether x == y or not. Since x = 0 and y = 1, then we get False because x == 1 would be True while x == y would be False. The program then checks whether x != y and since x = 0 and y = 1 again, we get True because x != 1 would be True while x != y would be False. Now if both these conditions fail, then we will get something like this: x is equal to y which happens to be our last condition. Hence we get such an output. But this only works when you know exactly what you are doing otherwise it could get very confusing!

Since we had ‘else’ at the end of our if-elif-else statement, we get the output “x is equal to y” when we run this code snippet and everything else gets printed in our else block. Let us now change our code snippet like this:

x = 0 y = 1 else: print("x is not equal to y") if x == y: print("x equals y") elif x != y: print("x is not equal to y") else: print("x is equal to y")

Now when we run this code snippet, we would see only one output “x equals y” instead of three outputs as before which tells us that python executes our if-elif-else statement from top to bottom and executes only one of them depending on the conditions that are satisfied by a variable! This is nothing but a simple flow chart which you can see in the following image:

































III: Conclusion

In this tutorial, we looked at how to use ‘if’, ‘elif’ and ‘else’ statements in python programming examples. In your next python project or assignment it would definitely come handy!

loader
Course content