Python if-elif-else statements – with example

  • Reading time:16 mins read

Python if-elif-else statements

Conditional statements like Python if-elif-else are an essential part of decision-making in any programming language. In Python, conditional statements allow us to evaluate various conditions and execute statements or a group of statements depending on whether the condition was True or False.

This article will explore how we can use the if, elif, and else conditional statements in Python. 

 

 

Python if-elif-else – the if statement

The if statement is the most fundamental conditional statement and is often used alongside the else statement. The if statement allows us to execute code in its body only if the expression under test returns True

The if statement follows the following syntax:

 

if expression:
    statement or group of statements

 

The statements or group of statements are executed if the expression returns a boolean value of True. The indentation signifies the beginning of the body of the if statement.

 

python if-elif-else

 

In the example below, we use the if statement to test if variable y is greater than variable x. And if True, the print statement in the body of the if statement is executed.

 

 

In the example above, variable y is indeed greater than x; therefore, the body of the if statement is executed. Suppose we change the value of the variable x to 25. This change will make the above statement False. Therefore the print function under the if statement is not executed.

 

 

Python if-elif-else – The elif statement

An elif statement is a short form of the else if statement. This conditional statement allows us to check for multiple expressions. 

 

Python if-elif-else

 

If the expression under the if statement evaluates to False, the program proceeds to check expressions in other subsequent elif blocks. For example, we can check if variable x is greater than variable y and print a message and if it’s not greater than y, we can check if it is less than y and print a different message.

 

 

Since the expression under the, if statement is False, the print statement in its body is ignored. On the other hand, since the expression under the elif statement is True, its body’s print statement is executed.

An if block can have multiple elif statements, this means that you can test even more conditions.

 

python if-elif-else

 

Python if-elif-else – the else statement

The else statement is typically used alongside the if statement. An if statement can only have one else statement. When used alongside the elif statements, its body is executed when all conditions in the if and elif statements are False

 

For example, if we change the x and y variable to be equal, the if and elif statements will be False.

 

 

In this case, we can add an else statement that will be executed when all statements are False.

 

 

If one of the first two statements is True, then the else statement is not executed. For example, if we change the value of variable x to 25. That means that the elif statement will now be True, and thus the else part is not executed.

 

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.