Python – Decision Making ( IF ELSE Statement )

You have to make Decision if the conditions occurred during the execution of any program and specified action should be taken according to the conditions. You have to determine which action should be taken and which statements to execute if the outcome is TRUE or FALSE otherwise.

Python programming language assumes any non-null value as TRUE and any null value as FALSE. It provides the following types of decision-making statements.

  • if statement – if statement comprises an expression is followed by one or more statements.
  • if-else statement – Here if statement is followed by else statement which only executes when the first expression is FALSE.
  • nested if statement – You can use one if or else-if statement inside another if or else-if statement and so on.

IF Statement

IF statement is similar to the other programming languages. The if statement contains a logical expression using that data is compared and a decision is made based on the result of the comparison.

The syntax of if statement as follows,

if condition :
    statement-1
    statement-2
    statement-3

If the boolean expression is TRUE then the block of statement inside if statement will be executed. In Python, statements in a block are uniformly indented after the : (colon) symbol. If boolean expression gets FALSE then the first set of code after the end of block is executed.

name=input("Enter Name:")
if name=="ravi" :
    print("Hello ravi, good morning")
print("How are you!!!")

Here in above example, when you you provide name ravi when it prompts then it will return “Hello ravi, good morning”. And if you provide name other than ravi then it returns “How are you!!!”

IF..ELSE Statement

An else statement can be followed with if statement. An else statement contains a block of code that only executes if the conditional expression in the if statement resolves to zero/null or FALSE value. The else statement is the optional statement.

The syntax of the if…else statement as follows

if condition :
    Action-1
else :
    Action-2

If the boolean expression is TRUE then the block of statement inside if statement will be executed.If boolean expression gets FALSE then block of code inside else statement will be executed.

name=input("Enter Name:")
if name=="ravi" :
    print("Hello ravi, good morning")
else:
    print("How are you!!!")

Here in above example, when you you provide name ravi when it prompts then it will return “Hello ravi, good morning”. And if you provide name other than ravi then it will return “How are you!!!” that is inside else statement.

IF…ELIF…ELSE Statement

If you have multiple conditions then you can use if..elif..else statement in your code. An elif statement contains a block of code that only executes if the conditional expression in the if statement gets FALSE value and before else statement.

The syntax of the if…elif..else statement as follows

if condition1:
    Action-1
elif condition2:
    Action-2
elif condition3:
    Action-3
elif condition4:
    Action-4
...
else:
    Default Action

Here is the example of nested if statement in Python.

amt=int(input("Enter the amount: "))

if amount < 10:
    discount = amount*0.50
    print ("Discount", discount)
elif amount<5000:
    discount = amount*0.10
    print ("Discount", discount)
else:
    discount = amount*0.25
    print ("Discount", discount)

print ("Net payable amount :",amount-discount)

Nested IF Statement

Many times there is a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if statement in your code.

The syntax of the nested if…elif…else as follows,

if expression1:
    statement(s)
    if expression2:
        statement(s)
    elif expression3:
        statement(s)
    else
        statement(s)
elif expression4:
    statement(s)
else:
    statement(s)

Here is the example of nested if statement in Python.

x = 10
if x > 5:
    print("Above 5," )
    if x > 20:
        print("above 20!")
    else:
        print("below 20.")

Pass attribute with If statement

You cannot If statements empty. if for some reason you have an if statement without any content then you should put pass statement to avoid getting an error.

x = 10
y = 20
if b > a:
    pass

Read Also : Python Dictionaries

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “Python – Decision Making ( IF ELSE Statement )”

  1. Pingback: Python While Loops

Leave a Comment