if else Statement in Python Programming
Decision Making in Python
In this lesson, we will understand what if else statement is and how it works in Python programming along with some example.
What is if else Statement
In Python program the if statement alone tells us that if a condition is true then it will execute a block of statements and if the condition is false it won’t. But what-if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
if else Statement Syntax
if condition:
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
In the above syntax after if statement we will write our condition with a colon : at the end. If the condition is true then the statements written below the if statement (using tab indentation) will execute otherwise the statements written below the else: statement (using tab indentation) will execute.
Now let's see an example for more understanding.
Example
Python program to check if an integer variable's value is even or odd number.
a=15
if a%2==0:
print(a,'is an even number')
else:
print(a,'is an odd number')
Output
15 is an odd number
Here you can see that the condition a%2==0 is false because the remainder of the modulus division a%2 is not equal to 0. So the statement which is written below the else: statement (using tab indentation) has executed and the output is printed on the screen.
Nested if Statement
In Python program a nested if is an if statement which is inside the block of another if statement. Nested if statements means an if statement inside another if statement.
Nested if Statement Syntax
if condition1:
# Executes when condition1 is true
if condition2:
# Executes when condition2 is true
In the above syntax, you can see that we have written second if statement (using tab indentation) below the first if statement. If condition1 is true then condition2 will be checked, if condition2 is also true then the statement written below second if statement (using tab indentation) will execute.
Now let's see an example for more understanding.
Example
Python program to check if an integer variable's value is even and is also greater than 20 using nested if statement.
a=24
if a%2==0:
if a>20:
print(a,'is an even number and is also greater than 20')
else:
print(a,'is an even number but not greater than 20')
else:
print(a,'is an odd number')
Output
24 is an even number and is also greater than 20
Here you can see that the first condition a%2==0 is true because the remainder of the modulus division a%2 is equal to 0. So the second if statement which is written below the first if statement (using tab indentation) has executed and the second condition a>20 is also true so statement which is written below the second if statement (using tab indentation) has executed and the output is printed on the screen.
Test Your Knowledge
Attempt the practical questions to check if the lesson is adequately clear to you.