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

In your programming journey, decision making will be with you from the beginning to the end. At every stage, you need to make certain decisions based on a condition.

That is what we will be discussing in this Python decision making article and learn to use if statements, if-else statements, if-elif ladder and Nested statements in Python.

Decision-Making Statements in Python

There comes a point in your life where you need to decide what steps should be taken and based on that you decide your next decisions.

In programming, we often have this similar situation where we need to decide which block of code should be executed based on a condition.

Let’s take a simple example.

Suppose you are writing a program for a game. So at every step, we need to take decisions like:

  • If the user presses ‘w’ key, then the character will move forward.
  • If the user presses ‘spacebar’, then the character will jump.
  • If the character runs into an obstacle, then the game is over otherwise we continue playing.

Decisions like these are required everywhere in programming and they decide the direction of flow of program execution.

Python has the following decision-making statements:

  • if statements
  • if-else statements
  • if-elif ladder
  • Nested statements

Let’s discuss these decision making statements in Python in detail.

Python if statement

if statement is the most simple form of decision-making statement. It takes an expression and checks if the expression evaluates to True then the block of code in if statement will be executed.

If the expression evaluates to False, then the block of code is skipped.

Syntax:

if ( expression ):
  Statement 1
  Statement 2
  .
  Statement n

python decision making - if statement

Example 1:

a = 20 ; b = 20
if ( a == b ):
  print( “a and b are equal”)
print(“If block ended”)

Output:

a and b are equal
If block ended

Example 2:

num = 5
if ( num >= 10):
  print(“num is greater than 10”)
print(“if block ended”)

Output:

If block ended

In example 1, we see that the condition a==b evaluates to True. Therefore, the block of code inside if statement is executed.

In example 2, the condition evaluates to False, therefore, the print statement was not executed and the only statement that got executed was because it was outside the if block.

Note: Don’t forget to add a colon(:) after if statement and indent the statements properly that are executed when a condition is True.

Python if-else statement

From the name itself, we get the clue that the if-else statement checks the expression and executes the if block when the expression is True otherwise it will execute the else block of code. The else block should be right after if block and it is executed when the expression is False.

Syntax:

if( expression ):
  Statement
else:
  Statement

decision making in python if-else statement

Example 1:

number1 = 20 ; number2 = 30
if(number1 >= number2 ):
  print(“number 1 is greater than number 2”)
else:
  print(“number 2 is greater than number 1”)

Output:

number 2 is greater than number 1

Note: Only one else statement is followed by an if statement. If you use two else statements after an if statement, then you get the following error.

Example:

if (5>10):
  print(5)
else:
  print(10)
else:
  print(“End”)

Output:

SyntaxError: invalid syntax

Python if-elif ladder

You might have heard of the else-if statements in other languages like C/C++ or Java.

In Python, we have an elif keyword to chain multiple conditions one after another. With elif ladder, we can make complex decision-making statements.

The elif statement helps you to check multiple expressions and it executes the code as soon as one of the conditions evaluates to True.

Syntax:

if( expression1 ):
  statement
elif (expression2 ) :
  statement
elif(expression3 ):
  statement
.
.
else:
  statement

python if-elif ladder

Example:

print(“Select your ride:”)
print(“1. Bike”)
print(“2. Car”)
print(“3. SUV”)

choice = int( input() )

if( choice == 1 ):
  print( “You have selected Bike” )
elif( choice == 2 ):
  print( “You have selected Car” )
elif( choice == 3 ):
  print( “You have selected SUV” )
else:
  print(“Wrong choice!“)

Output 1:

Select your ride:
1. Bike
2. Car
3. SUV
3
You have selected SUV

Output 2:

Select your ride:
1. Bike
2. Car
3. SUV
10
Wrong choice!

Note: Core Python doesn’t support switch-case statements that are available in other programming languages but we can use the elif ladder instead of switch cases.

Python Nested if statement

In very simple words, Nested if statements is an if statement inside another if statement. Python allows us to stack any number of if statements inside the block of another if statements. They are useful when we need to make a series of decisions.

Syntax:

if (expression):
  if(expression):
    Statement of nested if
  else:
    Statement of nested if else
  Statement of outer if
Statement outside if block

nested if statement in python decision making

Example :

num1 = int( input())
num2 = int( input())

if( num1>= num2):
    if(num1 == num2):
        print(f'{num1} and {num2} are equal')
    else:
        print(f'{num1} is greater than {num2}')
else:
    print(f'{num1} is smaller than {num2}')

Output 1:

10
20
10 is smaller than 20

Output 2:

5
5
5 and 5 are equal

Summary

Today in decision making in python article, we learned how a computer program can make decisions using any condition. We learned how to use decision-making statements like if, if-else, nested if and if-elif ladder.

The keynote to keep in mind is to use proper indentation to declare the block of code of these statements.