Decision making is an essential concept in any programming language and is required when you want to execute code when a specific condition is satisfied. In this blog, you will learn about the famous if-else statement in Python. We’ll be using Jupyter Notebook to demonstrate the code.
There are multiple forms of if-else statements. Let’s explore them one by one.
It uses the if keyword, followed by the condition.
#statement to execute if the condition is true
Below is the entire workflow of how if statements work:
First, the test expression is checked. If the expression is true, the body of the if the statement is executed. If it is false, the statement present after the if statement is executed. In either case, any line of code present outside if the statement is evaluated by default.
To understand this better, we’ll use an example:
print("This is the if body")
print("This is outside the if block")
Since 20 is not greater than 50, the statement present inside the if block will not execute. Instead, the statement present outside the if block is executed.
In the code below, both the print statements will be executed since a is greater than 50.
So far, we could specify the statements that will be executed if a condition is true. Now, if you want to evaluate statements that determine whether a condition is actual and if a separate set of statements is false, you can use the if-else conditional statement.
The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.
#Executes this block if the condition is true
#Executes this block if the condition is false
You should note here that Python uses indentation in both the blocks to define the scope of the code. Other programming languages often use curly brackets for this purpose.
Below is the entire workflow of how the if-else statement works.
First, the test expression is checked. If it is true, the statements present in the body of the if block will execute. Next, the statements present below the if block is executed. In case the test expression has false results, the statements present in the else body are executed, and then the statements below the if-else are executed.
The following is an example that better illustrates how if-else works:
Since the value of “i” is divisible by two, the if statement is executed.
Since the value of “i” is not divisible by two, the else statement is executed.
Let us now look at what a nested IF statement is and how it works.
Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course.
Nested IF Statement
When an if a statement is present inside another if statement, it is called a nested IF statement. This situation occurs when you have to filter a variable multiple times.
#Executes if condition1 is true
#Executes if condition2 is true
#Condition2 ends here
#Condition1 ends here
In nested IF statements, you should always take care of the indentation to define the scope of each statement. You can have as many levels of nesting as required, but it makes the program less optimized, and as a result, can be more complex to read and understand. Therefore, you should always try to minimize the use of nested IF statements.
The workflow below demonstrates how nested IF statements work:
The following is another example that shows how nested IF works: We have a number, and we’re going to check if the number is greater or less than 25. If the number is less than 25, we’ll check if it is an odd number or an even number. If the number is greater than 25, we will print that the number is greater than 25.
So far, with IF and if-else, we have only seen a binary approach. Suppose we have a problem that has multiple conditions. In this scenario, the if-elif-else statement comes to the rescue.
It checks the if statement condition. If that is false, the elif statement is evaluated. In case the elif condition is false, the else statement is evaluated.
Below is a flowchart that shows how the if-elif-else ladder works. The Test Expression1 is checked. If that proves true, the body of if is evaluated. If it is false, then the control moves to the proceeding Test Expression2. If it’s true, the body of elif1 is executed. If it’s false, the test expression3 is checked. If true, the body of elif2 is executed. If it is false, the body of else is evaluated. Any statement below in if-elif is then checked.
The program below uses the if-elif-else ladder to check if a letter is a vowel or a consonant.
Now that we have looked at the basics of if, else, elif and nested IF, let’s do some exercises.
Program to Check the Greatest among Three Numbers
In the above code, we first check if 'a' is higher than both 'b' and 'c'. Next, we print 'a' as the greatest number.
In case it is false, we then check if 'b' is greater than both 'a' and 'c'. If this is true, we print 'b' as the greatest number. Otherwise, 'c' is the greatest number.
The same program can be created using the nested IF statement as follows:
Here is one more exercise that can allow you to check whether a number is divisible by two, three, or five.
The problem with the code above is that 12 is also divisible by three, but we are unable to print it. In that case, we only need to use the if statement.
The last output statement is incorrect since the output is divisible by two and three. To fix this issue, use a counter variable.
Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.
I hope this blog helped you understand conditional statements in Python. You learned about if, else, if-elif-else and nested IF statements and practiced with a few hands-on exercises. To learn more, watch this Python If Else Statement. To get more in-depth training in Python programming, take our Python Training Course.