Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.
Understanding Python If-Else Statement

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.

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

If Statement

It uses the if keyword, followed by the condition.

Syntax:

if condition:

#statement to execute if the condition is true

Below is the entire workflow of how if statements work:

test-expression

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:

a=20

if a>50:

    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.

if-block.

In the code below, both the print statements will be executed since a is greater than 50.

outside-if

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.

Learn from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

If-else 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.

Syntax:

if(condition):

#Executes this block if the condition is true

else:

#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.

false

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.

ifblock-even

The following is an example that better illustrates how if-else works:

ifblock-odd.

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.

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.

Syntax:

if (condition1):

    #Executes if condition1 is true

    if (condition2):

        #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:

test-expression-1

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.

c-evenodd

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.

If-Elif-Else Statement

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.

Syntax:

if (condition):

    statement

elif (condition):

    statement

.

.

else:

    Statement

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.

test-expression-123

The program below uses the if-elif-else ladder to check if a letter is a vowel or a consonant.

var

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

 greatest-number

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:

greatest-number-2

Here is one more exercise that can allow you to check whether a number is divisible by two, three, or five.

divisible-by

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.

not-divisible.

The last output statement is incorrect since the output is divisible by two and three. To fix this issue, use a counter variable.

counter-variable

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

Conclusion

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.

About the Author

Avijeet BiswalAvijeet Biswal

Avijeet is a Senior Research Analyst at Simplilearn. Passionate about Data Analytics, Machine Learning, and Deep Learning, Avijeet is also interested in politics, cricket, and football.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.