Python Conditionals

Conditional statements in Python allow code to execute based on specific conditions. They are essential for making decisions within programs, handling different outcomes based on varying data, and implementing control flows. Python supports the following conditional statements:

1. `if` Statement
2. `if-else` Statement
3. `if-elif-else` Statement
4. Nested Conditionals
5. `match` Statement (Python 3.10+)

1. `if` Statement

The `if` statement evaluates a condition. If the condition is `True`, the indented block of code under `if` is executed. If the condition is `False`, the block is skipped.
age = 18

# Basic if statement
if age >= 18:
    print("You are eligible to vote.")

Output:
You are eligible to vote.


2. `if-else` Statement

An `if-else` statement allows a choice between two options. If the condition is `True`, the `if` block is executed. If `False`, the `else` block executes.
number = 10

# if-else statement
if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Output:
The number is even.


3. `if-elif-else` Statement

The `if-elif-else` statement allows multiple conditions to be evaluated in sequence. The program executes the first block where the condition is `True`. If none are `True`, it executes the `else` block.
score = 85

# if-elif-else statement
if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Output:
Grade: B


4. Nested Conditionals

Nested conditionals are `if` statements inside another `if` or `else` block. They allow for more complex decision-making by adding multiple levels of conditions.
age = 20
has_id = True

# Nested conditional statements
if age >= 18:
    if has_id:
        print("Access granted.")
    else:
        print("ID required for verification.")
else:
    print("Access denied. You must be 18 or older.")

Output:
Access granted.


5. Ternary Conditional (Conditional Expressions)

A ternary conditional is a compact way to write `if-else` statements in a single line. It is useful for simple decisions.
age = 16
result = "Eligible to vote" if age >= 18 else "Not eligible to vote"
print(result)

Output:
Not eligible to vote


6. `match` Statement (Python 3.10+)

The `match` statement provides a pattern-matching approach similar to `switch` in other languages. It allows matching specific values or data structures with patterns.
status_code = 404

# match statement example
match status_code:
    case 200:
        print("OK")
    case 404:
        print("Not Found")
    case 500:
        print("Server Error")
    case _:
        print("Unknown Status Code")

Output:
Not Found


Combining Conditions with Logical Operators

Python supports logical operators (`and`, `or`, `not`) to combine multiple conditions in a single `if` statement.
age = 25
has_license = True

# Combining conditions with 'and'
if age >= 18 and has_license:
    print("You are allowed to drive.")

# Combining conditions with 'or'
has_permit = False
if has_license or has_permit:
    print("You can drive with a permit or license.")

# Using 'not' for negation
if not has_permit:
    print("You cannot drive without a permit or license.")

Output:
You are allowed to drive.
You can drive with a permit or license.
You cannot drive without a permit or license.


Summary

Python's conditional statements allow for flexible and powerful decision-making in code. Understanding and utilizing `if`, `if-else`, `if-elif-else`, nested conditionals, ternary conditionals, and the `match` statement (for Python 3.10+) are fundamental to creating interactive and responsive Python programs.

Previous: Python Numbers | Next: Python range and xrange

<
>