If-Elif-Else Condition in Python | Definition , Syntax and Example program

Definition  of   If-Elif-Else Condition : 

In Python, the if-elif-else condition allows  to check multiple conditions and execute different blocks of code depending on which condition is true. It provides more flexibility than just if-else, as it allows checking multiple conditions in sequence.


  • if: Executes code if the first condition is true.
  • elif: Executes code if the previous conditions are false, but this condition is true.
  • else: Executes code if all the above conditions are false.



Syntax:

if condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition1 is False and condition2 is True else: # Code to execute if all conditions are False


Example Program:

number = 10 if number > 15: print("Number is greater than 15") elif number > 5: print("Number is greater than 5 but less than or equal to 15") else: print("Number is 5 or less")

Output:

Number is greater than 5 but less than or equal to 15

Post a Comment

0 Comments