Decision Making in Python

Decision Making : 

There comes some situtaion where we need to take some decison , on that decision  further execution  of the code depends.

Decision making statements decides the output or result of a program. In python we can make decision using –

  • If Statement
  • If -Else statements
  • Nested If staements
  • If-Elif Ladder 
  • Short- Hand if-else statement
  • Short-Hand If statement
Decision making statements in python

If-Statement :

  • If statement is the simplest decision making statemet in Python programming. It is use to decide weather a particular statement is True or not.
  • If the condition holds true , the statemet inside the’IF’ will execute else it will not. 
Syntax :

  if( Condition ) :

      #statements
#pyhton program 
#if-statement
me=‘Prepster’
if(me==‘Prepster’):
    print(‘Welcome to PrepInsta’)
Output :

Welcome to PrepInsta

If-Else Statement : 

  • If we want to do something  else when the condition inside ‘If’ is False , here we use if-else.
  • If condition holds true , then program will execute the code under the if-condition.
  • If condition is False , it will execute the code under else part. 
Syntax : 

  if( condition ):

    #statements

  else:

      #statements
decision making-if-else in-python
#pyhton program 
#if-else statement
me=‘Xyz’
if(me==‘Prepster’):
    print(‘Welcome to PrepInsta’
else :
    print(‘You are welcome to become a Prepster.’)
Output :

You are welcome to become a Prepster.

Nested If : 

  • Nested if statement means an If statement under another If statement  and Python allows us to do So.
  • If we have to take some decisions on the basis of two or more condition  we can use nested-if statements.
Synatx : 

  if ( condition1):

      #statements

      if(condition2):

          #statements 

      #block ends here 

  #blocks end here
#pyhton program 
#nested if statement
me=‘Xyz’
if(me):
    print(‘Welcome to PrepInsta’)
    if(me==‘Xyz’):
        print(‘You are welcome to become a Prepster.’)
Output :

Welcome to PrepInsta
You are welcome to become a Prepster.

If-elif Ladder : 

  • When we have multiple condition to check and excute according to the given condition , we can use if-elif-ladder.
  • It’s upto us that we want to use a else portion in the code.
  • It follows top-down approach.

 

Syntax : 

  if(condition1):

      #statements 

elif(condition2): 

    #statements 

  .

  .

  .

  . 

  else:

      #statements
#pyhton program 
#if-elif statement
me=
if(me==‘Prepster’):
    print(‘Welcome to PrepInsta’)
    
elif(me!=‘PrepSter’ and len(me)>0):
    print(‘You are welcome to become a Prepster.’
else:
    print(‘You should not be here.’

 

Output :

You should not be here.

Short Hand If statement :

  • Whenever there is only one statement inside the If loop.
  • The statement can be put on the same line as the if statement. 

 

Syntax : 

  if(condition): #statement

Short Hand If -else statement :

  • Whenever there is only one statement inside the both If-else loop.
  • The statement can be put on the same line as the if -else statement. 
Syntax : 

#staement1 for True  if(condition) else  #statement2 for False.
#pyhton program 
#Shirt hand if statement
me=‘Xyz’
if(me==‘Xyz’): print(‘You are welcome to become a Prepster.’

#Shirt hand if-else statement
me=‘Prepster’
print(‘You are welcome to become a Prepster.’if(me==‘Xyz’else print(‘Welcome To PrepInsta’)

 


Output :

You are welcome to become a Prepster.
Welcome To PrepInsta