Python - If Elif Else Statement

In Python there are three types of Control Statements if, if_else and if_elif_else.

As long as the expression given by these control statements is not true, then it is its own statement; does not execute

If the expression in true if statement is true then it executes its statement and if expression is false then flow goes to elif if expression of elif; If true, its statement is executed and the expression of elif; is false then else statement is executed.



Syntax for if_else Statement

if expression:
 if_statement (s)
elif expression:
 elif_statement (s)
else:
 else_statement (s)


Example for if_else Statement

Source Code:

x = 3
y = 3

if (x <y):
    print ("x is less than y")
elif (x> y):
    print ("x is greater than y")
else:
    print ("x is equal to y")
if elif else statement

Output:


x is equal to y