Python If Else Statement
In this article, we are going to learn about Python if else statement. Python if else statement is used to execute different statements based on the condition. We will look at it in detail. Topics covered in this article are:
- Python if statement
- elif python
- Python else
- Python ternary operator
- Python if elif else (recap)
Table Of Contents
Python if statement
While programming we generally need to make decisions based on certain conditions. For example, print a message if the number is greater than 10. In such cases, we use the if statement.
if statement in Python is a conditional statement. It is used to make decisions based on a given condition.
The statement accepts a condition and if the condition is true then it executes a code block.
Python if syntax
The syntax of the if statement is as follows:
if condition:
# statement
# statement
...
The 'condition' above is any valid Python expression.
The condition is checked before the code block is executed. If the condition is true, then the code block is executed. If the condition is false, then the code block is skipped.
if statement in python example
Let's see a few examples of if statement in python.
Example 1:
if 5 > 2: # execute code block if condition is true
print("5 is greater than 2")
Output:
5 is greater than 2
Example 2:
You can also directly give boolean value to the condition, like this:
if True: # always execute
print("True")
if False: # never execute
print("False")
Output:
True
Example 3:
Using Python expression as condition:
a, b = 10, 20
if (a + b) > ((a * b) / (a - b)): # python expression
print("a + b is greater than a * b / (a - b)")
Output:
(a + b) is greater than ((a * b) / (a - b))
Python if multiple conditions
In above all examples we have taken decisions based on a single condition. But can also take decisions based on multiple conditions using if statement.
Suppose we want to print a message if the number is greater than 10 and less than 20. We can do it like this:
num = 15
# multiple conditions
if num > 10 and num < 20:
print("15 is greater than 10 and less than 20")
Output:
15 is greater than 10 and less than 20
Using or
operator you can execute a code block if either of the condition is true.
num = 15
# multiple conditions
if num > 10 or num == 20:
print("15 is greater than 10 or less than 20")
Output:
15 is greater than 10 or less than 20
In the above code 'num greater than 10' is true but 'num equal to 20' is false. Since one of the condition is true, the code block is executed.
Nested if in python
You can write an if statement inside another if statement. This is called nested if.
Although you can use multiple conditions to get the same result it's worth knowing the nested if statement.
The following example shows the nested if statement in python.
num = 15
# nested if
if num > 10:
# another if
if num < 20:
print("15 is greater than 10 and less than 20")
Output:
15 is greater than 10 and less than 20
elif python
In the if statement we can run a block of code when the condition is true. But what if we want to check other conditions as well to execute some other code block?
In such cases, we can use the elif statement.
The elif statement is used to check the condition after the if statement. If the condition is false, then the next elif statement is checked.
Python elif syntax
The syntax of elif statement is as follows:
if condition_1:
# statement
...
elif condition_2:
# statement
...
The 'condition_1' and 'condition_2' above are any valid Python expressions.
Remember: elif statement can't be used independently. It must be used with an if statement.
Example 1:
num = 15
if num > 20: # False
print("15 is greater than 20")
elif num < 20: # True
print("15 is less than 20")
Output:
15 is less than 20
In the above example condition in 'if' statement is false. So the 'elif' statement is checked. If the condition is true, then the code block is executed.
Example 2:
You can use as many elif statements as you want.
age = 32
if age < 10:
print("Child")
elif age > 18:
print("Adult")
elif age > 30:
print("Old")
Output:
Old
Condition of 'if' statement is false, so next 'elif' statement is checked but still it is false. So the next 'elif' statement is checked which is true, so the code block is executed.
Python else
else is used to execute code block when all the conditions are false. It is optional.
You can use the else statement at the end of the if statement as a final option when none of the conditions are true.
Python else syntax
The syntax of the else statement is as follows:
if condition_1:
# statement
...
elif condition_2:
# statement
...
else:
# statement
...
The 'condition_1' are 'condition_2' are conditions. 'else' statement has no condition.
Remember: else statement can't be used independently and always comes after if statement. It is not necessary to use 'elif' to use else.
Example 1:
num = 15
if num > 15:
print("15 is greater than 15")
elif num < 15:
print("15 is less than 15")
else:
print("15 is equal to 15")
Output:
15 is equal to 15
In the above example, all the above conditions are false. So the 'else' statement is executed.
Example 2:
marks = 35
if marks > 90:
print("A+")
elif marks > 80:
print("A")
elif marks > 70:
print("B")
elif marks > 40:
print("C")
else:
print("F")
Output:
F
Python ternary operator
Ternary operator in python is used to check the condition and return the value based on the condition.
It is one line if-else statement. Mostly used to assign a value to a variable based on the condition.
Python ternary operator syntax
The syntax of the ternary operator is as follows:
value1 if condition else value2
The above expression returns the value of 'value1' if the condition is true. Otherwise, it returns the value of 'value2'.
Example 1:
a = 10
b = 20
min = a if a < b else b
print(min)
Output:
10
Example 2:
item = "fruit"
recipe = "Salad" if (item == "veg") else "Shake"
print(recipe)
Output:
Shake
Python if elif else (recap)
Conclusion
Conditional statements are used to check the condition and execute the code block based on the condition. Python has 4 types of conditional statements: if, elif, else, and ternary operator.