Wednesday, June 29, 2022

Python Conditional Statement - if, elif, else Statements

Conditional statement is used to execute a set of statements based on whether the condition is true or not. For conditional execution of statements if statement is used in Python. You can also have conditional branching using if-else and if-elif-else statements in Python.

Python if-elif-else statement syntax

General syntax for if-elif-else statement in Python is as follows-

if boolean_expression1:
elif boolean_expression2:
elif boolean_expressionN:

Note that there can be zero or more elif statements, even else clause is optional. So we’ll start with if statement examples and move towards full conditional branching using if-elif-else statement in Python.

if statement in Python

if statement is used to execute the body of code when the condition evaluates to true. If it is false then the indented statements (if suite) is not executed.

Python if statement syntax

if condition:

Here condition is a boolean expression that evaluates to either true or false. If it evaluates to true if-suite is executed, if expression evaluates to false then the suite if not executed.

Python if statement example

def myfunction():
    x = 10
    y = 12
    if x < y:
        print('x is less than y')
    print('After executing if statement')



x is less than y
After executing if statement

Indentation for grouping statements in Python

To understand conditional statements and loops in Python better you should also have an understanding of how Python used indentation for grouping statements.

Indentation refers to the white spaces at the beginning of the statement. In Python statements that are indented to the same level are part of the same group known as suite.

By default Python uses 4 spaces for indentation which is configurable.

Indentation in the context of if statement can be explained as given below-

Python if else statement

if-else statement in Python

Moving on from a simple if statement towards conditional branching there is if-else statement in Python. In if-else statement if-suite is executed if the condition evaluates to true otherwise else-suite is executed.

Python if-else statement syntax

if condition:

Python if-else statement example

def myfunction():
    x = 15
    y = 12
    if x < y:
        diff = y - x
        print('In if difference is-', diff)
        diff = x - y
        print('In else difference is-', diff)

    print('After executing if-else statement')



In else difference is- 3
After executing if-else statement

Condition (x < y) evaluates to false therefore else statement is executed.

if-elif-else statement in Python

If you have conditional branching based on multiple conditions then you can use if-elif-else statement in Python.

Python if-elif-else statement syntax

if condition1:
elif condition2:
elif condition3:
elif conditionN:

Initially condition1 is evaluated if it is true then if-suite is executed, if condition1 is false then condition2 is evaluated if that is true then elif-suite-2 is executed. If condition 2 is false then condition 3 is evaluated and so on. If none of the condition is true then else-suite is executed.

Python if-elif-else statement example

def myfunction():
    invoice_amount = 2000
    tarrif = 0
    if invoice_amount > 2500 and zone == "Americas":
        tarrif = 100
    elif invoice_amount > 2500 and zone == "EU":
        tarrif = 150
    elif invoice_amount > 2500 and zone == "APAC":
        tarrif = 200
        tarrif = 250
    print('Tarrif is', tarrif)



Tarrif is 250

Nested if statement in Python

You can have nested if-elif-else statement with in if statement upto any depth, only thing is that each nested if statement has to be indented properly.

Nested if statement Python example

def myfunction():
    x = 65
    if x >= 50:
        # nested if statement
        if x > 75 and x <= 99:
            print('x is greater than 75 but less than 100')
        elif x >= 50 and x <= 75:
             print('x is greater than or equal to 50 but less than or equal to 75')
            print('x is greater than 100')
        print('x is less than 50')



x is greater than or equal to 50 but less than or equal to 75

That's all for this topic Python Conditional Statement - if, elif, else Statements. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page

Related Topics

  1. Python while Loop With Examples
  2. Ternary Operator in Python
  3. Abstraction in Python
  4. Magic Methods in Python With Examples
  5. Python Functions : Returning Multiple Values

You may also like-

  1. Abstract Class in Python
  2. Python String join() Method
  3. Python Program to Check Armstrong Number
  4. Automatic Numeric Type Promotion in Java
  5. Java join() Method - Joining Strings
  6. equals() And hashCode() Methods in Java
  7. Spring depends-on Attribute and @DependsOn With Examples
  8. Angular First App - Hello world Example

No comments:

Post a Comment