Python if elif else

In this article, we will see Python if elif else functionality with examples.

The Python if elif else statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evalautes to TRUE.

Python if elif else Syntax:

if expression: statement1: statement2: elif expression2: statement3: statement4: elif expression3: statement5: statement6: else: statements

Python if elif else Examples:

1. Check given number is greater than, less than or equal to zero.

x=int(input("Enter the value for x: ")) if x>0: print("Positive") elif x<0: print("Negative") else print("Zero")

Python if elif else

2. Find the grade of student using Python if elif else statement

marks=int(input("Enter the valid student marks: ")) if marks>=90: print("grade is A+") elif marks>=80: print("grade is A") elif marks>=70: print("grade is B") elif marks>=60: print("grade is B-") elif marks>=50: print("grade is C") elif marks>=40: print("Just Pass") else: print("Failed")