top of page
Writer's pictureThe Tech Platform

What is elif in Python?

When it comes to writing programs in Python, efficient decision-making is a crucial aspect. The ability to guide your code's flow based on certain conditions can greatly enhance its functionality and flexibility. In Python, the if, elif, and else statements are fundamental tools for implementing conditional logic. In this article, we'll delve into the specifics of the elif statement and explore how it contributes to building structured and organized code.


Table of content:

Syntax

2. Examples

What is elif in Python?

Elif in Python is short for "else if" and is used to check for another condition if the first if statement is false. It is a conditional statement used to execute a block of code if a certain condition is met. The elif statement is followed by a Boolean expression, and if the expression is true, the block of code is executed. If the expression is false, the next elif statement is evaluated, and so on. If none of the elif statements are true, the else statement is executed.


Syntax:

The syntax for an elif in Python is as follows:

if condition 1:   
    # do something
elif condition 2:   
    # do something else
else:   
    # do something else

The condition 1 is the first condition that you want to check. If the condition is true, the block of code inside the if statement is executed. If the condition is false, the elif statement is evaluated.


The condition 2 is the second condition that you want to check. If the condition is true, the block of code inside the elif statement is executed. If the condition is false, the else statement is executed.


What is elif in Python

Examples:

Below, we present two examples that illustrate the usage of elif in Python:


Example 1: Determine the age range of a person

In this example, we'll use the elif statement to categorize individuals into different age groups based on their input age. The user is prompted to input their age, and the program then evaluates the age against a series of conditions to determine whether they are a child, teenager, or adult.

# Get user input for age
age = int(input("Please enter your age: "))

if age >= 18:
    print("You are an adult.")
elif age >= 13:
    print("You are a teenager.")
else:
    print("You are a child.")
elif in Python: Example 1

Example 2: Check if a number is even or odd

In this example, we demonstrate the use of the elif statement to check whether a user-inputted number is even or odd. The program takes a number as input and then uses the modulo operator to determine its parity, providing an appropriate message based on the result.

# Get user input for number
number = int(input("Please enter a number: "))

if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")
elif in Python: Example 2

Elif statements with nested if statements in Python

When writing programs, especially those involving decision-making, Python's conditional statements provide a powerful mechanism for guiding the program's behavior based on various conditions. One commonly used structure is the "if-elif-else" construct, where "elif" stands for "else if." This construct allows you to evaluate multiple conditions in sequence and execute corresponding blocks of code based on the first condition that is true.


Consider a scenario where you want to provide feedback on a student's grade. Depending on the grade received, you want to display a message that reflects the level of achievement.

grade = input("Please enter your grade: ")

if grade == "A":
  print("You got an excellent grade!")
elif grade == "B":
  print("You got a good grade!")
elif grade == "C":
  print("You got a satisfactory grade!")
elif grade == "D":
  print("You got a passing grade!")
else:
  print("You failed the course!")

In this example, the first if statement checks if the variable age is greater than or equal to 18. If it is, the

In this example, the first if statement checks if the variable grade is equal to "A".


If it is, the block of code inside the first if statement is executed, which prints the message "You got an excellent grade!" The first if statement then checks if the variable grade is equal to "B". If it is, the block of code inside the first elif statement is executed, which prints the message "You got a good grade!" If the variable grade is not equal to "A" or "B", the second elif statement is executed. The second elif statement checks if the variable grade is equal to "C". If it is, the block of code inside the second elif statement is executed, which prints the message "You got a satisfactory grade!" If the variable grade is not equal to "A", "B", or "C", the third elif statement is executed. The third elif statement checks if the variable grade is equal to "D". If it is, the block of code inside the third elif statement is executed, which prints the message "You got a passing grade!" If the variable grade is not equal to any of the previous values, the else statement is executed, which prints the message "You failed the course!"


The output will be:

Elif in Python with nested If statement

When to use elif in Python?

Here are some situations where you might want to use an elif in Python:

  • When you need to check for more than one condition. For example, you could use an elif statement to determine the age range of a person and then print out a different message for each age range.

  • When you need to make a decision based on different input values. For example, you could use an elif statement to determine the type of a number and then perform a different operation on the number depending on its type.

  • When you want to make your code more concise and easier to understand. For example, you could use an elif statement instead of multiple nested if statements.

When not to use elif in Python?

Here are some situations where you might not want to use an elif in Python:

  • When you only have one condition to check. In this case, it is better to use a single if statement.

  • When the conditions in your elif statement are not mutually exclusive. This means that it is possible for more than one condition to be true at the same time. In this case, you should use a switch statement instead of an elif statement.

  • When your elif statement is becoming too complex. If your elif statement is becoming too long or difficult to read, it might be a sign that you need to refactor your code.


Conclusion

The elif in Python provides a valuable tool for handling multiple conditions in an organized and efficient manner. By allowing you to sequence through various possibilities, the elif construct enhances your code's responsiveness and adaptability. This feature empowers you to make informed decisions, guiding your programs along different paths based on the situation at hand. As you continue to explore and implement elif statements, you'll gain a deeper understanding of how to create dynamic and effective code structures.

Recent Posts

See All

Comments


bottom of page