Decision Making in Python

Decision Making in Python: 

Decision Making in Python is primarily accomplished using conditional statements. Conditional statements allow you to execute different blocks of code based on whether a certain condition or expression is True or False. Python provides several ways to perform decision-making, with the most common ones being the if, elif (else if), and else statements.

Decision making statements in python

Decision Making in Python

There might come some situation where we need to take some decision , 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 statements
  • If-Elif Ladder 
  • Short- Hand if-else statement
  • Short-Hand If statement

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
Run
#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
Run
#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
Run
#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
Run
#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
Run
#Short hand if statement
me='Xyz'
if(me=='Xyz'): 
print('You are welcome to become a Prepster.')
Output:

You are welcome to become a Prepster.

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.

Run

#Short hand if-else statement
me='Prepster'
if(me=='Xyz'): print('You are welcome to become a Prepster.')
else:
print('Welcome To PrepInsta')
Output :
Welcome To PrepInsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription