All Courses

Decision Making in Python

Pallavi Dhotre

2 years ago

Decision Making in Python | insideAIML
Table of Content
Introduction
Decision making in Python
Decision making statements in python
  • if statement ( Single alternative decision)
  • if-else statement (Double alternative decision)
  • if-elif-else statements (Multiple alternative decisions)
  • Nested if-else statement
Summary

Introduction 

          Do you know how to Decision Making in python works? Before that, let’s understand the need for a decision.  In day to day activity, there are certain situations where you need to make the decisions. But these decisions are based on the result of the condition. For example, your boss has instructed you that you can leave the office early if you finish the task. In this example condition is taskFinished==True.  Based on the result of this condition next action to be performed is dependent. Suppose the condition is true then you can leave the office early, if the condition is false then you cannot leave the office early. This is an example of decision-making. In this article, we are going to discuss in-depth python decision making, decision making statements in python, and python decision making statements with examples. 

Decision making in Python

          Condition checking is an integral part of decision making in python. Conditions are simply Boolean expressions. And the decision is depending on the result of the Boolean expression. The result of the Boolean expression is a Boolean value, which is the most important part of decision making in python. 

Decision making statements in python (Types, Examples)

          Following are some decision making statements in python. 
  • if statement (single alternative decisions)
  • if-else statement (double alternative decisions)
  • if-elif-else statements (multiple alternative decisions)
  • nested if-else statement
let’s have a brief look at the decision making statements in python

if statement ( Single alternative decision)

          Single alternative decisions have only one block of the statements and that executes only if the condition results true. There is no else part in this statement. 
Syntax
if condition :
               Block1 
For example: Write a program that will print a message “Welcome ” if your system is turned on. And draw the flowchart for the same.
Program:
#example of Decision making statements in python
#if statement (single-alternative)
systemIsOn=True       #Assume system is on
if systemIsOn==True:  #checking the condition
    print("Welcome")  #condition is true then print the message
Output:
Welcome
Flowchart
Flowchart of if statement | insideAIML

if-else statement (Double alternative decision) 

          In this type of structure, there are two blocks of statement. If the condition is true then the first block gets executed and if the condition is false then the second block gets executed. There is an if part and an else part for this type of statement.
Syntax
if condition :
                  block of statements1
else:
                block of statements2
For example: Write a program that will print a message “Welcome ” if your system is turned on else it will print “Turn on your system”. And draw the flowchart for the same.
Program 
#example of Decision making statements in python
#if-else statement (dual-alternative)
systemIsOn=input()      
if systemIsOn==True:  #checking the condition
    print("Welcome")  #condition is true then execute this part
else:                 #if the condition is false
    print("Turn on your system") #execute this part
Output
False
Turn on your system
Flowchart
Flowchart of if-else statement | insideAIML

if-elif-else statements (Multiple alternative decisions)

          In the program, if there are multiple decisions involved then we use the elif keyword. Suppose for printing the day if a day is 1 then print Monday if a day is 2 then print Tuesday if a day is 7 then print Sunday. In a similar way, there are many problems where multiple alternative decisions are needed. 
Syntax
if condition1 :
                block of statements1
elif condition2:
              block of statements2
else:
            block of statements3
For example: Write a program to accept a three-digit number from the user. If the number is divisible by 100 then print “Divisible by 100”, else if the number is divisible by 50 then print “Divisible by 50”, else if the number is divisible by 20 then print “Divisible by 20”, and else if the number is divisible by 10 then print “Divisible by 10”. Otherwise, print “Try again”. Design a flowchart for the program.
Program:
#example of Decision making statements in python
#if-elif-else statement (multiple-alternative)
number=int(input("Enter the 3-digit number: "))
if number%100==0:
    print("Divisible by 100")
elif number%50==0:
    print("Divisible by 50")
elif number%20==0:
    print("Divisible by 20")
elif number%10==0:
    print("Divisible by 10")
else:
    print("Try again")
Output 
Enter the 3-digit number: 200

Divisible by 100
Flowchart
Flowchart of if-elif-else statements | insideAIML

Nested if-else statement

          If-else structure under another if-else structure is called nested if-else statement. 
Syntax
if condition1 :
                 block of statements1
else:
                 if condition2:
                                   block of statements2
                else:
                                  block of statements3
For example: Write a program to accept a three-digit number from the user. If the number is divisible by 100 then print “Divisible by 100”, else if the number is divisible by 50 then print “Divisible by 50”, else, print “Try again”. Also, draw the flowchart for the same.
Program
#example of Decision making statements in python
#nested statement 
number=int(input("Enter the 3-digit number: "))
if number%100==0:
    print("Divisible by 100")
else:
    if number%50==0:
        print("Divisible by 50")
    else:
        print("Try again")
Output
Enter the 3-digit number: 135

Try again
Flowchart
Flowchart of Nested if-else statement | insideAIML

Summary

          In this article, we discussed decision making in python and decision making statements in python. The decision is depending on the result of the Boolean expression. The result of the Boolean expression is a Boolean value, which is the most important part of decision making in python. Multiple blocks of statements can be executed but it depends on the result of a Boolean expression.  We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below. 
    
Like the Blog, then Share it with your friends and colleagues to make this AI community stronger. 
To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our blog page - https://insideaiml.com/blog
Keep Learning. Keep Growing.
    

Submit Review