How to Use else if Statements in Python?

In Python, the else if condition is represented by the elif keyword. It allows you to check multiple conditions in sequence.

Basic Syntax

if condition1:
    # Code block
elif condition2:
    # Code block
else:
    # Code block

Example Usage

score = 85
if score >= 90:
    print('Grade A')
elif score >= 80:
    print('Grade B')
elif score >= 70:
    print('Grade C')
else:
    print('Grade D')

Multiple elif Conditions

You can include as many elif statements as needed:

if condition1:
    pass
elif condition2:
    pass
elif condition3:
    pass
else:
    pass

Conclusion

The elif keyword is essential for checking multiple conditions in Python. It enhances the decision-making capabilities of your programs.

Leave a Reply

Your email address will not be published. Required fields are marked *