1. Home
  2. Docs
  3. Programming with Python
  4. Control Statements
  5. Conditional Statements

Conditional Statements

Conditional statements, also known as Selection statements, are control statements that allows a program to make decisions and execute certain blocks of code only when specific conditions are met.

  • Conditional statements evaluate expressions (conditions) that return either true or false and perform actions based on these evaluations.

Types of Conditional Statements in Python:

  • The if Statement
  • The if-else statement
  • The if-elif-else statement
  • The match-case Statement
  • The if statement is used to execute a block of code if a condition is true. If the condition is false, it is skipped.

Syntax:

if condition:
    # Code to execute if the condition is True

Example:

x = 10
if x > 0:
    print("x is positive")
  • The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.

Syntax:

if condition:
    # Code to execute if the condition is True
else:
    # Code to execute if the condition is False

Example:

x = -5
if x > 0:
    print("x is positive")
else:
    print("x is not positive")

The if-elif-else statement is used when you have multiple conditions to check. It allows you to test multiple conditions and execute code based on the first true condition.

Syntax:

if condition1:
    # Code to execute if condition1 is True
elif condition2:
    # Code to execute if condition2 is True
elif condition3:
    # Code to execute if condition3 is True
else:
    # Code to execute if none of the above conditions are True

Example:

x = 0
if x > 0:
    print("x is positive")
elif x = = 0:
    print("x is zero")
else:
    print("x is negative")

The match-case statement is a pattern-matching feature introduced in Python 3.10. It allows for cleaner and more readable code for scenarios where multiple conditions need to be checked, similar to a switch statement in other languages.

Syntax:

match variable:
    case pattern1:
        # Code to execute if variable matches pattern1
    case pattern2:
        # Code to execute if variable matches pattern2
    case _:
        # Code to execute if no patterns match (default case)

Example:

day = "Monday"

match day:
    case "Monday":
        print("Start of the workweek!")
    case "Friday":
        print("Almost the weekend!")
    case "Saturday" | "Sunday":
        print("Weekend vibes!")
    case _:
        print("Midweek grind!")

In Python, you can use an if-else statement in a compact form known as a ternary operator (or conditional expression). The ternary operator allows you to write a single line of code to evaluate a condition and return a value based on whether the condition is True or False.

Syntax of Ternary Operator:

value_if_true if condition else value_if_false
  • condition: The expression to evaluate.
  • value_if_true: The result if the condition is True.
  • value_if_false: The result if the condition is False.

Example:

age = 20
status = "Adult" if age > = 18 else "Minor"
print(status)

#   output
 #  Adult

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *