If Elif
Python - if, elif, else Conditions
Section titled “Python - if, elif, else Conditions”By default, statements in the script are executed sequentially from the first to the last. If the processing logic requires so, the sequential flow can be altered in two ways:
Python uses the if keyword to implement decision control. Python’s syntax for executing a block conditionally is as below:
if
if [boolean expression]: statement1 statement2 ... statementNAny Boolean expression evaluating to True or False appears after the if keyword. Use the : symbol and press Enter after the expression to start a block with an increased indent. One or more statements written with the same level of indent will be executed if the Boolean expression evaluates to True.
True``False``if``:``if``True
To end the block, decrease the indentation. Subsequent statements after the block will be executed out of the if condition. The following example demonstrates the if condition.
if``if
price = 50
if price < 100: print("price is less than 100")`price = 50
if price < 100: print(“price is less than 100”)`Try it
price is less than 100price is less than 100
In the above example, the expression price < 100 evaluates to True, so it will execute the block. The if block starts from the new line after : and all the statements under the if condition starts with an increased indentation, either space or tab. Above, the if block contains only one statement. The following example has multiple statements in the if condition.
price < 100``True``if``:``if``if
price = 50quantity = 5
if price*quantity < 500: print("price*quantity is less than 500") print("price = ", price) print("quantity = ", quantity)`price = 50 quantity = 5
if pricequantity < 500: print(“pricequantity is less than 500”) print(“price = ”, price) print(“quantity = ”, quantity)`Try it
price*quantity is less than 500 price = 50 quantity = 5price*quantity is less than 500 price = 50 quantity = 5
Above, the if condition contains multiple statements with the same indentation. If all the statements are not in the same indentation, either space or a tab then it will raise an IdentationError.
IdentationError
price = 50quantity = 5if price*quantity < 500: print("price is less than 500") print("price = ", price) print("quantity = ", quantity)price = 50 quantity = 5 if price*quantity < 500: print("price is less than 500") print("price = ", price) print("quantity = ", quantity)Try it
print("quantity = ", quantity) ^ IdentationError: unexpected indent print("quantity = ", quantity) ^ IdentationError: unexpected indent
The statements with the same indentation level as if condition will not consider in the if block. They will consider out of the if condition.
if``if
price = 50quantity = 5if price*quantity < 100: print("price is less than 500") print("price = ", price) print("quantity = ", quantity)print("No if block executed.")price = 50 quantity = 5 if price*quantity < 100: print("price is less than 500") print("price = ", price) print("quantity = ", quantity) print("No if block executed.")Try it
No if block executed.No if block executed.
The following example demonstrates multiple if conditions.
price = 100
if price > 100: print("price is greater than 100")
if price == 100: print("price is 100")
if price < 100: print("price is less than 100")`price = 100
if price > 100: print(“price is greater than 100”)
if price == 100: print(“price is 100”)
if price < 100: print(“price is less than 100”)`Try it
price is 100price is 100
Notice that each if block contains a statement in a different indentation, and that’s valid because they are different from each other.
if
else Condition
Section titled “else Condition”Along with the if statement, the else condition can be optionally used to define an alternate block of statements to be executed if the boolean expression in the if condition evaluates to False.
if``else``if``False
if [boolean expression]: statement1 statement2 ... statementN else: statement1 statement2 ... statementNAs mentioned before, the indented block starts after the : symbol, after the boolean expression. It will get executed when the condition is True. We have another block that should be executed when the if condition is False. First, complete the if block by a backspace and write else, put add the : symbol in front of the new block to begin it, and add the required statements in the block.
:``True``if``False``if``else``:
price = 50
if price >= 100: print("price is greater than 100")else: print("price is less than 100")`price = 50
if price >= 100: print(“price is greater than 100”) else: print(“price is less than 100”)`Try it
price is less than 100price is less than 100
In the above example, the if condition price >= 100 is False, so the else block will be executed. The else block can also contain multiple statements with the same indentation; otherwise, it will raise the IndentationError.
price >= 100``False``else``IndentationError
Note that you cannot have multiple else blocks, and it must be the last block.
else
elif Condition
Section titled “elif Condition”Use the elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions.
elif``if``if``else
if [boolean expression]: [statements] elif [boolean expresion]: [statements] elif [boolean expresion]: [statements] else: [statements]The elif block is executed if the specified condition evaluates to True.
elif``True
price = 100
if price > 100: print("price is greater than 100")elif price == 100: print("price is 100")elif price < 100: print("price is less than 100")`price = 100
if price > 100: print(“price is greater than 100”) elif price == 100: print(“price is 100”) elif price < 100: print(“price is less than 100”)`Try it
price is 100price is 100
In the above example, the elif conditions are applied after the if condition. Python will evalute the if condition and if it evaluates to False then it will evalute the elif blocks and execute the elif block whose expression evaluates to True. If multiple elif conditions become True, then the first elif block will be executed.
elif``if``if``False``elif``elif``True``elif``True``elif
The following example demonstrates if, elif, and else conditions.
price = 50
if price > 100: print("price is greater than 100")elif price == 100: print("price is 100")else price < 100: print("price is less than 100")`price = 50
if price > 100: print(“price is greater than 100”) elif price == 100: print(“price is 100”) else price < 100: print(“price is less than 100”)`Try it
price is less than 100price is less than 100
All the if, elif, and else conditions must start from the same indentation level, otherwise it will raise the IndentationError.
IndentationError
price = 50
if price > 100: print("price is greater than 100") elif price == 100: print("price is 100") else price < 100: print("price is less than 100")`price = 50
if price > 100: print(“price is greater than 100”) elif price == 100: print(“price is 100”) else price < 100: print(“price is less than 100”)`Try it
elif price == 100: ^IdentationError: unindent does not match any outer indentation level elif price == 100: ^ IdentationError: unindent does not match any outer indentation level
Nested if, elif, else Conditions
Section titled “Nested if, elif, else Conditions”Python supports nested if, elif, and else condition. The inner condition must be with increased indentation than the outer condition, and all the statements under the one block should be with the same indentation.
price = 50quantity = 5amount = price*quantity
if amount > 100: if amount > 500: print("Amount is greater than 500") else: if amount <= 500 and amount >= 400: print("Amount is between 400 and 500") elif amount <= 400 and amount >= 300: print("Amount is between 300 and 400") else: print("Amount is between 200 and 300")elif amount == 100: print("Amount is 100")else: print("Amount is less than 100")`price = 50 quantity = 5 amount = price*quantity
if amount > 100: if amount > 500: print(“Amount is greater than 500”) else: if amount <= 500 and amount >= 400: print(“Amount is between 400 and 500”) elif amount <= 400 and amount >= 300: print(“Amount is between 300 and 400”) else: print(“Amount is between 200 and 300”) elif amount == 100: print(“Amount is 100”) else: print(“Amount is less than 100”)`Try it
Amount is between 200 and 500Amount is between 200 and 500