---Advertisement---

Python Conditional Statements Explained: IF…Else, ELIF & Switch Case with Examples Great 2025

By Bhavani

Updated On:

---Advertisement---
Python Conditional Statements Explained: IF...Else, ELIF & Switch Case with Examples Great 2025

👉Tutorial-2: Python For & While Loops
👉Tutorial-3: Python Break, Continue, and Pass Statements
👉Tutorial-4: Python OOPs Concepts
👉Tutorial-5: Polymorphism in Python with Examples
👉Tutorial-6:  Mutable vs Immutable Objects in Python

Python Conditional Statements: Conditional statements in Python are used to execute different code blocks based on whether a Boolean condition evaluates to True or False. These statements allow developers to create decision-making logic in Python programs.


Python Conditional Statements

An if statement is used to execute a block of code only if the specified condition is True. If the condition is False, the block is skipped.

python

if condition:

    # Code block for true condition

python

def main():

    x, y = 2, 8

    if x < y:

        st = “x is less than y”

    print(st)

if __name__ == “__main__”:

    main()

Python Conditional Statements

  • x and y are initialized as 2 and 8 respectively.
  • The if condition (x < y) is true, so the message “x is less than y” is printed.

The else block is used to execute code when the if condition evaluates to False.

python

if condition:

    # Code block for true condition

else:

    # Code block for false condition

python

def main():

    x, y = 8, 4

    if x < y:

        st = “x is less than y”

    else:

        st = “x is greater than y”

    print(st)

if __name__ == “__main__”:

    main()

  • Since x < y is False, the else block executes, printing “x is greater than y”.

The elif condition is used when there are multiple possible conditions.

python

if condition1:

    # Code block for condition1

elif condition2:

    # Code block for condition2

else:

    # Code block for all other conditions

python

def main():

    x, y = 8, 8

    if x < y:

        st = “x is less than y”

    elif x == y:

        st = “x is same as y”

    else:

        st = “x is greater than y”

    print(st)

if __name__ == “__main__”:

    main()

  • The first condition (x < y) is False.
  • The elif condition (x == y) is True, so the message “x is same as y” is printed.

Python also allows writing conditional statements in a concise way using inline syntax.

python

variable = value_if_true if condition else value_if_false

python

def main():

    x, y = 10, 8

    st = “x is less than y” if x < y else “x is greater than or equal to y”

    print(st)

if __name__ == “__main__”:

    main()

  • Since x > y, the output is “x is greater than or equal to y”.

A nested if statement is an if statement inside another if statement.

python

total = 100

country = “AU”

if country == “US”:

    if total <= 50:

        print(“Shipping Cost is $50”)

    elif total <= 100:

        print(“Shipping Cost is $25”)

    elif total <= 150:

        print(“Shipping Cost is $5”)

    else:

        print(“FREE”)

elif country == “AU”:

    if total <= 50:

        print(“Shipping Cost is $100”)

    else:

        print(“FREE”)

  • Based on the country value, the appropriate shipping cost is calculated.

Python does not have a direct switch statement, but similar functionality can be achieved using dictionaries.

python

def SwitchExample(argument):

    switcher = {

        0: “This is Case Zero”,

        1: “This is Case One”,

        2: “This is Case Two”

    }

    return switcher.get(argument, “nothing”)

if __name__ == “__main__”:

    argument = 1

    print(SwitchExample(argument))

  • The switcher.get() method efficiently mimics a switch-case structure in Python.

  • Python conditional statements allow you to control program flow using if, else, and elif.
  • The else block handles conditions that fail.
  • The elif block is used for additional conditions.
  • Python’s concise inline syntax makes conditions easier to manage.
  • Nested if statements help in multi-level decision-making.
  • While Python lacks a direct switch statement, dictionary mapping effectively achieves similar functionality.

Python 2D Arrays

Download Python

Leave a Comment

Index