Python elseif statement

Basic if-elif-else statement:

The program first tests if x > 10. If this condition is real, it executes the corresponding block. If no longer, it actions to the next condition, x == 10. If this situation is real, it executes its block. If none of the situations is genuine, the block below else is completed.

                                  
                                    x = 10

if x > 10:
    print("x is greater than 10")
elif x == 10:
    print("x is equal to 10")
else:
    print("x is less than 10")
                                  
                                

Multiple elif statements:

The program tests multiple situations sequentially, and the primary real condition encountered triggers the execution of its corresponding block.

                                  
                                    day = "Wednesday"

if day == "Monday":
    print("It's the start of the week")
elif day == "Wednesday":
    print("It's the middle of the week")
elif day == "Friday":
    print("It's the end of the week")
else:
    print("It's some other day")
                                  
                                

Nested if-elif-else statements:

You can nest if-elif-else statements interior each other to create extra complicated conditions. However, in lots of cases, the usage of nested systems can be averted through restructuring the situations.

                                      
                                        grade = 75

if grade >= 90:
    print("A")
elif grade >= 80:
    print("B")
else:
    if grade >= 70:
        print("C")
    else:
        print("F")
                                      
                                    

Practical Example:

The application assessments the temperature and prints a message primarily based on whether it is warm, moderate, or cold.

                                      
                                        temperature = 28

if temperature >= 30:
    print("It's hot!")
elif 20 <= temperature < 30:
    print("It's moderate.")
else:
    print("It's cold.")
                                      
                                    

The elif statement is beneficial when you need to check a couple of conditions in a controlled collection. It gives a clean and organized manner to address complicated selection-making in your Python applications.