Python If Else, If Elif Else, Nested If for Decision Making

This tutorial describes the use of conditional programming constructs such as Python if else and if-elif-else with the help of flowchart and examples.

You would also get to learn more complex forms of decision-making statements such as Nested if, use of ‘not,’ ‘in’ and ‘and’ operators.

A conditional statement (like if X > Y:) envelopes around a block of code. It lets Python evaluate the logical expression (X > Y) first. If the result is True, then only the code indented under the conditional block gets executed.

Let’s now briefly see what this tutorial is going to cover.

Table of Content

Go back to ☛ Python Tutorial

Python if Statement

A bare Python if statement evaluates whether an expression is True or False, and executes the underlying code only if the result is True.

Given below is the syntax of Python if statement.

Syntax

if Logical_Expression :
    Indented Code Block

Flowchart

Basic Python if Statement Flowchart

Basic Python if Statement Flowchart

Example

days = int(input("How many days in a leap year? "))
if days == 366:
    print("You have cleared the test.")
print("Congrats!")

The output of the above code is –

How many days in a leap year? 366
You have cleared the test.
Congrats!

Python if Else Statement

A Python if else statement takes action irrespective of what the value of the expression is.

If the result is True, then the code block following the expression would get executed. Otherwise, the code indented under the else clause would get run.

Given below is the syntax of Python if else statement.

Syntax

if Logical_Expression :
    Indented Code Block 1
else :
    Indented Code Block 2

Flowchart

Python If Else Statement Flowchart

Python If Else Statement Flowchart

Example

answer = input("Is Python an interpreted language? Yes or No >> ").lower()

if answer == "yes" :
    print("You have cleared the test.")
else :
    print("You have failed the test.")

print("Thanks!")

When you run the above code, it would ask for your input and convert it into the lower case to perform a case-insensitive comparison in the if-else condition.

If you enter a ‘yes,’ then the output of the above code would be –

Is Python an interpreted language? Yes or No >> yes
You have cleared the test.
Thanks!

If you enter a ‘no,’ then the output of the above code would be –

Is Python an interpreted language? Yes or No >> no
You have failed the test.
Thanks!

Python if-Elif-Else Statement

The previous two if-else constructs would address at most two outcomes, i.e., True or False. However, the expression next to the “if” statement can also evaluate to a different value other than the boolean. It means to have more conditions, not just a single “else” block.

Python supports to specify multiple conditions by using an “elif” clause with each of the expression.

Given below is the syntax of Python if-elif-else statement.

Syntax

if Logical_Expression_1 :
    Indented Code Block 1
elif Logical_Expression_2 :
    Indented Code Block 2
elif Logical_Expression_3 :
    Indented Code Block 3
...
else :
    Indented Code Block N

Flowchart

Python If Elif Else Statement Flowchart

Python If Elif Else Statement Flowchart

Example

while True:
    response = input("Which Python data type is an ordered sequence? ").lower()
    print("You entered:", response)
    
    if response == "list" :
        print("You have cleared the test.")
        break
    elif response == "tuple" :
        print("You have cleared the test.")
        break
    else :
        print("Your input is wrong. Please try again.")

This program has a while loop where it is querying about Python data types. It wants you to enter the name of an ordered sequence. If you provide a wrong value, then it would again prompt you for the correct input.

Only by entering the correct value, the loop breaks. However, you can also press the CTRL+C to exit the program.

Had you entered a wrong answer, then the output would be :

Which Python data type is an ordered sequence? dictionary
You entered: dictionary
Your input is wrong. Please try again.
Which Python data type is an ordered sequence?

Once you provide the correct answer, the program would end with the following output.

Which Python data type is an ordered sequence? tuple
You entered: tuple
You have cleared the test.

Nested If-Else in Python

Some programs may have a code block under an “if” clause which subsequently has another conditional block as the first statement.

In such a case, Python allows nesting of an if-else or if-elif-else inside another conditional clause.

Python doesn’t limit the level of nested conditions in a program. Given below is the syntax of a multi-level nested if-elif-else statement.

Syntax

if Logical_Expression_1 :
    if Logical_Expression_1.1 :
        if Logical_Expression_1.1.1 :
            Indented Code Block 1.1.1
        else
            Indented Code Block
    elif Logical_Expression_1.2 :
        Indented Code Block 1.2
    else :
        Indented Code Block
elif Logical_Expression_2 :
    Indented Code Block 2
elif Logical_Expression_3 :
    Indented Code Block 3
...
else :
    Indented Code Block

Flowchart

Python Nested If Else Statement Flowchart

Python Nested If Else Statement Flowchart

The above diagram represents the following code flow.

x = 10
y = 20
z = 30

print("Start")
if x == 10:
    print(" Nested If")
    if y == 20:
        print(" End of Nested If Block ")
    else:
        print(" End of Nested If-Else Block ")
elif y == 20:
    print(" Elif block ")
else:
    print(" Nested If")
    if z == 30:
        print(" End of Nested If Block ")
    else:
        print(" End of Nested If-Else Block ")
print("Stop")

Example

while True:
    response = int(input("How many days are there in a leap year? "))
    print("You entered:", response)
    
    if response == 366 :
        print("You have cleared the first level.")
        response = input("What month has an extra day in leap year?? ").lower()
        if response == "february" :
            print("You have cleared the test.")
            break
        else :
            print("You have failed the test.")
            break
    else :
        print("Your input is wrong, please try again.")

The above code demonstrates the use of nested if in Python. It first asks a question from the user. After that, there is an if statement to check whether the answer is correct or not. In case the user provides the right input, then he faces another question. Now, the nested if comes into the picture checking the latest response from the user.

Using Not Operator with Python If Else

The ‘not’ is a negation logical operator in Python. It reverses the result of its operand and converts to a boolean outcome, i.e., True or False. The operand could be a variable or an expression evaluating to a numeric type.

Example-1

a = 10
b = 20
if not a > b :
    print("The number %d is less than %d" %(a, b))/
The output of the above code is -
The number 10 is less than 20

Example-2

X = 0
if not X :
    print("X is not %d" %(X))
else :
    print("X is %d" %(X))

The output of the above code is –

X is not 0

Using And Operator with Python If Else

By using the ‘and’ operator, you can join multiple expression in a Python if condition. It is also a logical operator which evaluates as True if both/all the operands (x and y and z) are True.

Flowchart

You can find the code of this flowchart in the underlying example.

Using And Operator with Python If Else Elif

Using And Operator with Python If Else Elif

Example

Check out the following example to see the ‘and’ operator in action.

a = 10
b = 20
c = 30

avg = (a + b + c) / 3
print("avg =", avg)

if avg > a and avg > b and avg > c:
    print("%d is higher than %d, %d, %d" %(avg, a, b, c))
elif avg > a and avg > b:
    print("%d is higher than %d, %d" %(avg, a, b))
elif avg > a and avg > c:
    print("%d is higher than %d, %d" %(avg, a, c))
elif avg > b and avg > c:
    print("%d is higher than %d, %d" %(avg, b, c))
elif avg > a:
    print("%d is just higher than %d" %(avg, a))
elif avg > b:
    print("%d is just higher than %d" %(avg, b))
elif avg > c:
    print("%d is just higher than %d" %(avg, c))

The output of the above code is –

avg = 20.0
20 is just higher than 10

Using In Operator with Python If Else

Python “in” operator allows comparing a variable against multiple values in a single line. It makes decision making more comfortable by reducing the use of many if-elif statements.

In Python, we often refer to it as the membership operator. It can let you check value from objects of different types such as the list, tuple, string, dictionary types in Python.

Examples

This example first creates a list of six numbers. After that, there is a for loop which is traversing it and prints values.

The loop has an if statement which prints specific numbers from the list which are not in the tuple used in the condition.

Hence, we’ve also used the “not” along with the “in” operator.

#Example of "in" operator with Python If statement

num_list = [1, 10, 2, 20, 3, 30]
for num in num_list:
    if not num in (2, 3):
        print ("Allowed Item:", num)

The output of the above code is as follows.

Allowed Item: 1
Allowed Item: 10
Allowed Item: 20
Allowed Item: 30

Let’s check out another example which has two teams of players (team1 and team2) for two games. In here, we’ve to find who from the “team1” also plays for the “team2”.

# Find players who play both games

team1 =["Jake", "Allan", "Nick", "Alex", "Dave"]
team2 =["David", "John", "Chris", "Alex", "Nick"]

for aplayer in team1:
    if aplayer in team2:
        print("%s also plays for team2." %(aplayer))

The output of the above code is as follows.

Nick also plays for team2.
Alex also plays for team2.

Summary – Python If Else, If-Elif-Else, and Nested If Concepts

Yes, the software programs can make decisions at runtime. But their correctness depends on how effectively have you added the conditions.

In this tutorial, we covered the concepts of Python if else, if-elif-else and a couple of its variations using various Python operators.

If you found the explanation useful, then do share this tutorial with your colleagues. Also, connect to our social media (Facebook/Twitter) accounts to receive timely updates.

Best,

TechBeamers