What is the "elif" statement in Python and how to use it?

Table of Contants

Introduction:

The elif statement in Python stands for "else if" and is used to add multiple conditions to an if-else structure. It allows your program to evaluate a sequence of conditions, executing the first block of code where the condition is True. If none of the if or elif conditions are True, the else block (if present) is executed. The elif statement helps you handle complex logic with multiple decision points in a clean and readable way.

Syntax and Basic Usage of the elif Statement

The structure of an if-elif-else block looks like this:

  • if: Checks the first condition.
  • elif: Checks another condition if the previous if (or elif) condition is False.
  • else: A final block that runs if none of the conditions are True.

Example:

  • In this example, if age is less than 18, the program prints "You are a minor." If it's between 18 and 64, it prints "You are an adult." Otherwise, it prints "You are a senior citizen."

How the elif Statement Works

  1. The if condition is evaluated first.
  2. If the if condition is True, the corresponding block of code is executed, and the rest of the elif and else blocks are skipped.
  3. If the if condition is False, the program evaluates the first elif condition.
  4. This process continues for each elif condition until a True condition is found or all conditions have been evaluated.
  5. If none of the conditions are True, the else block is executed.

Example: Multiple Conditions Using elif

  • Here, the program checks each condition in sequence. Since score is 75, the condition score >= 70 is True, so it prints "Grade: C" and skips the rest of the conditions.

When to Use elif

You should use elif when you need to check multiple conditions and want to avoid nested if statements, which can make your code less readable. The elif structure is a cleaner and more efficient way to handle multiple decision points.

Example: Checking Multiple Categories

  • In this example, the program evaluates each condition. Since temperature is 30, the output is "It's warm."

Combining elif with Logical Operators

You can also combine conditions in the elif block using logical operators like and, or, and not to create more complex conditions.

Example:

  • In this example, multiple conditions are combined using the and operator in the elif block to differentiate between morning and afternoon on Sunday.

Practical Examples of Using elif

1. Using elif in Age Verification:

2. Handling Multiple Conditions with elif:

3. Grading System Using elif:

Conclusion:

The elif statement in Python is a versatile tool for handling multiple conditions in your programs. It works seamlessly with if and else to create efficient, readable decision-making structures. By using elif, you can avoid deeply nested if statements and write clean, easy-to-understand code. Whether you are building complex decision trees or handling simple conditional logic, the elif statement provides a powerful way to control the flow of your program.

Similar Questions