Decision Making in Python using if, if-else, if-elif and nested statements

That is where control structures come in. A control structure directs the order of execution of the statements in a program (referred to as the program’s control flow).

Here’s what you’ll learn in this tutorial: You’ll encounter your first Python control structure, the if statement.

In the real world, we commonly must evaluate information around us and then choose one course of action or another based on what we observe:

If the weather is nice, then I’ll mow the lawn. (It’s implied that if the weather isn’t nice, then I won’t mow the lawn.)

In a Python program, the if statement is how you perform this sort of decision-making. It allows for conditional execution of a statement or group of statements based on the value of an expression.

The outline of this tutorial is as follows:

  • First, you’ll get a quick overview of the if statement in its simplest form.
  • Next, using the if statement as a model, you’ll see why control structures require some mechanism for grouping statements together into compound statements or blocks. You’ll learn how this is done in Python.
  • Lastly, you’ll tie it all together and learn how to write complex decision-making code.

Introduction to the if Statement

We’ll start by looking at the most basic type of if statement. In its simplest form, it looks like this:

if <expr>:

In the form shown above:

  • <expr> is an expression evaluated in Boolean context, as discussed in the section on Logical Operators in the Operators and Expressions in Python tutorial.
  • <statement> is a valid Python statement, which must be indented. (You will see why very soon.)

If <expr> is true (evaluates to a value that is “truthy”), then <statement> is executed. If <expr> is false, then <statement> is skipped over and not executed.

Note that the colon (:) following <expr> is required. Some programming languages require <expr> to be enclosed in parentheses, but Python does not.

Here are several examples of this type of if statement:>>>

>>> x = 0
>>> y = 5

>>> if x < y:                            # Truthy
...     print('yes')
>>> if y < x:                            # Falsy
...     print('yes')

>>> if x:                                # Falsy
...     print('yes')
>>> if y:                                # Truthy
...     print('yes')

>>> if x or y:                           # Truthy
...     print('yes')
>>> if x and y:                          # Falsy
...     print('yes')

>>> if 'aul' in 'grault':                # Truthy
...     print('yes')
>>> if 'quux' in ['foo', 'bar', 'baz']:  # Falsy
...     print('yes')

Grouping Statements: Indentation and Blocks

So far, so good.

But let’s say you want to evaluate a condition and then do more than one thing if it is true:

If the weather is nice, then I will:

  • Mow the lawn
  • Weed the garden
  • Take the dog for a walk

(If the weather isn’t nice, then I won’t do any of these things.)

In all the examples shown above, each if <expr>: has been followed by only a single <statement>. There needs to be some way to say “If <expr> is true, do all of the following things.”

The usual approach taken by most programming languages is to define a syntactic device that groups multiple statements into one compound statement or block. A block is regarded syntactically as a single entity. When it is the target of an if statement, and <expr> is true, then all the statements in the block are executed. If <expr> is false, then none of them are.

Virtually all programming languages provide the capability to define blocks, but they don’t all provide it in the same way. Let’s see how Python does it.

Python: It’s All About the Indentation

Python follows a convention known as the off-side rule, a term coined by British computer scientist Peter J. Landin. (The term is taken from the offside law in association football.) Languages that adhere to the off-side rule define blocks by indentation. Python is one of a relatively small set of off-side rule languages.

Recall from the previous tutorial on Python program structure that indentation has special significance in a Python program. Now you know why: indentation is used to define compound statements or blocks. In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block.

Thus, a compound if statement in Python looks like this:

 1 if <expr>:
 2     <statement>
 3     <statement>
 4     ...
 5     <statement>
 6 <following_statement>

Here, all the statements at the matching indentation level (lines 2 to 5) are considered part of the same block. The entire block is executed if <expr> is true, or skipped over if <expr> is false. Either way, execution proceeds with <following_statement> (line 6) afterward.

Python conditional statement
Python Compound if Statement

Notice that there is no token that denotes the end of the block. Rather, the end of the block is indicated by a line that is indented less than the lines of the block itself.

Comparison Operators

<Less than
<=Less than or Equal
 ==Equal to
>=Greater than or Equal
>Greater than
!=Not equal

•Boolean expressions ask a question and produce a Yes or No result which we use to control program flow

•Boolean expressions using comparison operators  evaluate to – True / False – Yes / No

•Comparison operators look at variables but do not change the variables

Consider this script file

 1 if 'foo' in ['bar', 'baz', 'qux']:
 2     print('Expression was true')
 3     print('Executing statement in suite')
 4     print('...')
 5     print('Done.')
 6 print('After conditional')

Running produces this output:

After conditional

The four print() statements on lines 2 to 5 are indented to the same level as one another. They constitute the block that would be executed if the condition were true. But it is false, so all the statements in the block are skipped. After the end of the compound if statement has been reached (whether the statements in the block on lines 2 to 5 are executed or not), execution proceeds to the first statement having a lesser indentation level: the print() statement on line 6.

Blocks can be nested to arbitrary depth. Each indent defines a new block, and each outdent ends the preceding block. The resulting structure is straightforward, consistent, and intuitive.

Here is a more complicated script file called

# Does line execute?                        Yes    No
#                                           ---    --
if 'foo' in ['foo', 'bar', 'baz']:        #  x
    print('Outer condition is true')      #  x

    if 10 > 20:                           #  x
        print('Inner condition 1')        #        x

    print('Between inner conditions')     #  x

    if 10 < 20:                           #  x
        print('Inner condition 2')        #  x

    print('End of outer condition')       #  x
print('After outer condition')            #  x

The output generated when this script is run is shown below:

Outer condition is true
Between inner conditions
Inner condition 2
End of outer condition
After outer condition

The else and elif Clauses

PROGRAMMING LOGIC: if else statement in php

Comparison between IF ELSE and IF

Now you know how to use an if statement to conditionally execute a single statement or a block of several statements. It’s time to find out what else you can do.

Sometimes, you want to evaluate a condition and take one path if it is true but specify an alternative path if it is not. This is accomplished with an else clause:

if <expr>:

If <expr> is true, the first suite is executed, and the second is skipped. If <expr> is false, the first suite is skipped and the second is executed. Either way, execution then resumes after the second suite. Both suites are defined by indentation, as described above.

In this example, x is less than 50, so the first suite (lines 4 to 5) are executed, and the second suite (lines 7 to 8) are skipped:>>>

 1 >>> x = 20
 3 >>> if x < 50:
 4 ...     print('(first suite)')
 5 ...     print('x is small')
 6 ... else:
 7 ...     print('(second suite)')
 8 ...     print('x is large')
 9 ...
10 (first suite)
11 x is small

Here, on the other hand, x is greater than 50, so the first suite is passed over, and the second suite executed:>>>

 1 >>> x = 120
 2 >>>
 3 >>> if x < 50:
 4 ...     print('(first suite)')
 5 ...     print('x is small')
 6 ... else:
 7 ...     print('(second suite)')
 8 ...     print('x is large')
 9 ...
10 (second suite)
11 x is large

Else If Ladder or Else if Chain

C if else statement - javatpoint

There is also syntax for branching execution based on several alternatives. For this, use one or more elif (short for else if) clauses. Python evaluates each <expr> in turn and executes the suite corresponding to the first that is true. If none of the expressions are true, and an else clause is specified, then its suite is executed:

if <expr>:
elif <expr>:
elif <expr>:

An arbitrary number of elif clauses can be specified. The else clause is optional. If it is present, there can be only one, and it must be specified last:>>>

>>> name = 'Joe'
>>> if name == 'Fred':
...     print('Hello Fred')
... elif name == 'Xander':
...     print('Hello Xander')
... elif name == 'Joe':
...     print('Hello Joe')
... elif name == 'Arnold':
...     print('Hello Arnold')
... else:
...     print("I don't know who you are!")
Hello Joe

At most, one of the code blocks specified will be executed. If an else clause isn’t included, and all the conditions are false, then none of the blocks will be executed.

An if statement with elif clauses uses short-circuit evaluation, analogous to what you saw with the and and or operators. Once one of the expressions is found to be true and its block is executed, none of the remaining expressions are tested. This is demonstrated below:>>>

>>> var  # Not defined
Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
NameError: name 'var' is not defined

>>> if 'a' in 'bar':
...     print('foo')
... elif 1/0:
...     print("This won't happen")
... elif var:
...     print("This won't either")

The second expression contains a division by zero, and the third references an undefined variable var. Either would raise an error, but neither is evaluated because the first condition specified is true.

Python Nested if statements

Nested if...else statement in C - Codeforwin

We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.

Python Nested if Example

'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Positive number")
    print("Negative number")

Output 1

Enter a number: 5
Positive number

Output 2

Enter a number: -1
Negative number

Output 3

Enter a number: 0

EL IF and NESTED IF are different

One-Line if Statements

It is customary to write if <expr> on one line and <statement> indented on the following line like this:

if <expr>:

But it is permissible to write an entire if statement on one line. The following is functionally equivalent to the example above:

if <expr>: <statement>

There can even be more than one <statement> on the same line, separated by semicolons:

if <expr>: <statement_1>; <statement_2>; ...; <statement_n>

But what does this mean? There are two possible interpretations:

  1. If <expr> is true, execute <statement_1>.Then, execute <statement_2> ... <statement_n> unconditionally, irrespective of whether <expr> is true or not.
  2. If <expr> is true, execute all of <statement_1> ... <statement_n>. Otherwise, don’t execute any of them.

Python takes the latter interpretation. The semicolon separating the <statements> has higher precedence than the colon following <expr>—in computer lingo, the semicolon is said to bind more tightly than the colon. Thus, the <statements> are treated as a suite, and either all of them are executed, or none of them are:>>>

>>> if 'f' in 'foo': print('1'); print('2'); print('3')
>>> if 'z' in 'foo': print('1'); print('2'); print('3')

Multiple statements may be specified on the same line as an elif or else clause as well:>>>

>>> x = 2
>>> if x == 1: print('foo'); print('bar'); print('baz')
... elif x == 2: print('qux'); print('quux')
... else: print('corge'); print('grault')

>>> x = 3
>>> if x == 1: print('foo'); print('bar'); print('baz')
... elif x == 2: print('qux'); print('quux')
... else: print('corge'); print('grault')

While all of this works, and the interpreter allows it, it is generally discouraged on the grounds that it leads to poor readability, particularly for complex if statements. PEP 8 specifically recommends against it.

As usual, it is somewhat a matter of taste. Most people would find the following more visually appealing and easier to understand at first glance than the example above:>>>

>>> x = 3
>>> if x == 1:
...     print('foo')
...     print('bar')
...     print('baz')
... elif x == 2:
...     print('qux')
...     print('quux')
... else:
...     print('corge')
...     print('grault')

If an if statement is simple enough, though, putting it all on one line may be reasonable. Something like this probably wouldn’t raise anyone’s hackles too much:

debugging = True  # Set to True to turn debugging on.


if debugging: print('About to call function foo()')

Conditional Expressions (Python’s Ternary Operator)

Python supports one additional decision-making entity called a conditional expression. (It is also referred to as a conditional operator or ternary operator in various places in the Python documentation.) Conditional expressions were proposed for addition to the language in PEP 308 and green-lighted by Guido in 2005.

In its simplest form, the syntax of the conditional expression is as follows:

<expr1> if <conditional_expr> else <expr2>

This is different from the if statement forms listed above because it is not a control structure that directs the flow of program execution. It acts more like an operator that defines an expression. In the above example, <conditional_expr> is evaluated first. If it is true, the expression evaluates to <expr1>. If it is false, the expression evaluates to <expr2>.

Notice the non-obvious order: the middle expression is evaluated first, and based on that result, one of the expressions on the ends is returned. Here are some examples that will hopefully help clarify:>>>

>>> raining = False
>>> print("Let's go to the", 'beach' if not raining else 'library')
Let's go to the beach
>>> raining = True
>>> print("Let's go to the", 'beach' if not raining else 'library')
Let's go to the library

>>> age = 12
>>> s = 'minor' if age < 21 else 'adult'
>>> s

>>> 'yes' if ('qux' in ['foo', 'bar', 'baz']) else 'no'

A common use of the conditional expression is to select variable assignment. For example, suppose you want to find the larger of two numbers. Of course, there is a built-in function max() that does just this (and more) that you could use. But suppose you want to write your own code from scratch.

You could use a standard if statement with an else clause:>>>

>>> if a > b:
...     m = a
... else:
...     m = b

But a conditional expression is shorter and arguably more readable as well:>>>

>>> m = a if a > b else b

Remember that the conditional expression behaves like an expression syntactically. It can be used as part of a longer expression. The conditional expression has lower precedence than virtually all the other operators, so parentheses are needed to group it by itself.

In the following example, the + operator binds more tightly than the conditional expression, so 1 + x and y + 2 are evaluated first, followed by the conditional expression. The parentheses in the second case are unnecessary and do not change the result:>>>

>>> x = y = 40

>>> z = 1 + x if x > y else y + 2
>>> z

>>> z = (1 + x) if x > y else (y + 2)
>>> z

If you want the conditional expression to be evaluated first, you need to surround it with grouping parentheses. In the next example, (x if x > y else y) is evaluated first. The result is y, which is 40, so z is assigned 1 + 40 + 2 = 43:>>>

>>> x = y = 40

>>> z = 1 + (x if x > y else y) + 2
>>> z

If you are using a conditional expression as part of a larger expression, it probably is a good idea to use grouping parentheses for clarification even if they are not needed.

Conditional expressions also use short-circuit evaluation like compound logical expressions. Portions of a conditional expression are not evaluated if they don’t need to be.

In the expression <expr1> if <conditional_expr> else <expr2>:

  • If <conditional_expr> is true, <expr1> is returned and <expr2> is not evaluated.
  • If <conditional_expr> is false, <expr2> is returned and <expr1> is not evaluated.

As before, you can verify this by using terms that would raise an error:>>>

>>> 'foo' if True else 1/0
>>> 1/0 if False else 'bar'

In both cases, the 1/0 terms are not evaluated, so no exception is raised.

Conditional expressions can also be chained together, as a sort of alternative if/elif/else structure, as shown here:>>>

>>> s = ('foo' if (x == 1) else
...      'bar' if (x == 2) else
...      'baz' if (x == 3) else
...      'qux' if (x == 4) else
...      'quux'
... )
>>> s

It’s not clear that this has any significant advantage over the corresponding if/elif/else statement, but it is syntactically correct Python.


With the completion of this tutorial, you are beginning to write Python code that goes beyond simple sequential execution:

  • You were introduced to the concept of control structures. These are compound statements that alter program control flow—the order of execution of program statements.
  • You learned how to group individual statements together into a block or suite.
  • You encountered your first control structure, the if statement, which makes it possible to conditionally execute a statement or block based on evaluation of program data.

All of these concepts are crucial to developing more complex Python code.

The next two tutorials will present two new control structures: the while statement and the for statement. These structures facilitate iteration, execution of a statement or block of statements repeatedly.

Like it? Share with your friends!

What's Your Reaction?

hate hate
confused confused
fail fail
fun fun
geeky geeky
love love
lol lol
omg omg
win win
Choose A Format
Personality quiz
Series of questions that intends to reveal something about the personality
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Voting to make decisions or determine opinions
Formatted Text with Embeds and Visuals
The Classic Internet Listicles
The Classic Internet Countdowns
Open List
Submit your own item and vote up for the best submission
Ranked List
Upvote or downvote to decide the best list item
Upload your own images to make custom memes
Youtube and Vimeo Embeds
Soundcloud or Mixcloud Embeds
Photo or GIF
GIF format