In Python, The elif keyword is used as one of the conditional statements, and it stands for else if. When we have a set of conditional statements to check, we use elif. This keyword must be used along with an if statement, or else it will result in an error.
SyntaxFollowing is the syntax of Python eilf keyword −
if condition1: statement1 statement2 elif condition2: statement1 statement2Example
Following is a basic example of Python elif keyword −
if False: print("This statement is not executed as the given condition is False") elif True: print("This statement is executed as the given condition is True")
Output
Following is the output of the above code −
This statement is executed as the given condition is TrueExample
In this example lets try to understand elif keyword when there are more than two condition. The condition mentioned in the if statement is false then eilf statements are to be executed until it reaches the True condition and remaining statements will not be executed −
var1=14 var2=100 if var1>var2: print(var1,"is not greater than ",var2) elif var1==var2: print(var1,"is not equal to ",var2) elif var1<var2: print(var1,"is less than ",var2)
Output
Following is the output of the above code −
14 is less than 100Using 'elif' keyword in Function
If there are more than one condition to check in a function we can use elif keyword and it will be return a value, if the condition is True.
ExampleLets understand elif keyword in function with an example −
def num(x): if x>0: print(x,"is a natural number") elif x<0: print(x,"is an negative number") elif x%2==0: print(x,"is a even number") var1=8 num(var1) var2=-6 num(var2)
Output
Following is the output of the above code −
8 is a natural number -6 is an negative numberUsing 'elif' without if condition
The elif keyword is used for additional conditions after an if statement. The elif statement will only be checked if the preceding if statement evaluates to False. Without an if statement, elif cannot be used and will results in SyntaxError.
ExampleLets understand see with an example when elif is used without if statement −
elif True: print("This will be not executed")
Output
Following is the output of the above code −
ERROR! Traceback (most recent call last): File "<main.py>", line 1 elif True: ^^^^ SyntaxError: invalid syntaxNested elif
If there are multiple else-if statements along with if statement with in a single block of code is known as nested-elif. If there is False condition in the outside elif then the nested-elif block will not be executed.
ExampleHere, is an example for nested elif. In the if statement we have given a False condition so nested elif block is executed−
if False: print("Hello World") if True: print("Welcome to Tutorials Point") elif True: print("Lets Learn Python") elif True: print("Hello") if False: print("Welcome") elif True: print("Tutorials Point")
Output
Following is the output of the above code −
Hello Tutorials Point
python_keywords.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4