Home » Elif in Python

Elif in Python

Elif in Python

Python Elif Statement

Sometimes, the programmer may want to evaluate more than one condition, this can be done using an python elif statement.

An elif statement works on the following principle,

  1. execute the block of code inside if statement if the initial expression evaluates to True. After execution return to the code out of the if block.
  2. execute the block of code inside the first elif statement if the expression inside it evaluates True. After execution return to the code out of the if block.
  3. execute the block of code inside the second elif statement if the expression inside it evaluates True. After execution return to the code out of the if block….
  4. execute the block of code inside the nth elif statement if the expression inside it evaluates True. After execution return to the code out of the if block.
  5. execute the block of code inside else statement if none of the expression evaluates to True. After execution return to the code out of the if block.
python-elif.png

Python elif Example

num = 0
if (nuzm < 0):
    print("Number is negative.")
elif (num == 0):
    print("Number is Zero.")
else:
    print("Number is positive.")
Python

Output:

Number is Zero.
Python

Let’s see some real-life examples to understand how python elif works:

  1. Grade Classification: score = 75 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F") In this example, based on the score, the program assigns a grade to a student. If the score is 75, it falls into the elif score >= 70: condition, printing “Grade: C”.
  2. Time of Day Greetings:
hour = 15

if hour < 12:
    print("Good morning!")
elif hour < 18:
    print("Good afternoon!")
else:
    print("Good evening!")
Python

Here, depending on the hour, the program greets the user appropriately. If it’s 15 (3 PM), it falls into the elif hour < 18: condition, printing “Good afternoon!”.

  1. Ticket Pricing: age = 25 is_student = False if age < 18: print("Child ticket: $8") elif age >= 65: print("Senior ticket: $10") elif is_student: print("Student ticket: $12") else: print("Standard ticket: $15") This example determines the ticket price based on the age and whether the person is a student. If the person is 25 years old and not a student, it falls into the else: condition, printing “Standard ticket: $15”.

In each of these examples, the elif statement allows us to check additional conditions after the initial if condition, providing a structured way to handle multiple cases in our code, similar to how we make decisions based on various conditions in real-life scenarios.

Conclusion

The elif statement in Python is a powerful tool for building conditional logic in your programs. It allows you to handle multiple conditions in a structured and readable way. By using elif, you can create programs that respond dynamically to different scenarios, making your code more flexible and adaptable. Remember to write clear and concise conditions, and always test your code with various inputs to ensure it behaves as expected. With practice, you’ll become proficient in using elif statements to solve a wide range of programming problems.

Frequently Asked Questions

Q1. What does elif stand for in Python?

Ans: elif stands for “else if”. It is used to check additional conditions after the initial if condition evaluates to false.


Q2. Can I use multiple elif statements in a single if block?

Ans: Yes, you can have multiple elif statements in a single if block to check multiple conditions sequentially.


Q3. What happens if multiple elif conditions are true?

Ans: In Python, only the block of code associated with the first true elif condition encountered will be executed. Subsequent elif blocks are skipped.


Q4. Is elif mandatory after an if statement?

Ans: No, elif is optional. You can have just an if block without any elif or else blocks.


Q5. Can I use elif without an else block?

Ans: Yes, you can use elif without an else block. If none of the if or elif conditions are true, the program will simply continue execution after the elif block.


Q6. Can I nest elif statements within each other?

Yes, you can nest elif statements within each other to create more complex decision-making structures.


Q7. How does elif differ from else?

Ans: elif is used to check additional conditions, whereas else is used as a catch-all for conditions that are not met by the if or elif blocks.