All articles
2 min read

Mastering Decision Making in Python: if, else, and elif

A complete guide to Python's conditional statements: if, elif, else, ternary expressions, truthy/falsy values, chained comparisons, and pattern matching with match/case.

Conditionals are the foundation of program logic — every decision your program makes flows through an if statement. Python keeps the syntax clean but offers several powerful patterns beyond the basics that help you write more expressive, readable code.

The if Statement

x = 10
if x > 5:
    print("x is greater than 5")

If the condition evaluates to True, the indented block runs. Python uses indentation (not braces) to define blocks — consistent 4-space indentation is the standard.

Adding else

x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")

Multiple Conditions with elif

age = 18

if age < 13:
    print("Child")
elif age < 18:
    print("Teenager")
elif age < 65:
    print("Adult")
else:
    print("Senior")

Python checks each condition in order and executes the first matching block. Once a match is found, the remaining elif and else branches are skipped.

Truthy and Falsy Values

Python conditionals don't require explicit == True checks. These values are falsy (treated as False in a boolean context):

  • None
  • 0, 0.0, 0j
  • Empty sequences: "", [], (), {}
  • Empty sets and dicts: set(), {}

Everything else is truthy. This lets you write:

name = input("Enter name: ")
if name:  # True if name is non-empty
    print(f"Hello, {name}")
else:
    print("No name provided")

items = []
if not items:
    print("List is empty")

Ternary (Conditional) Expressions

For simple one-liner assignments, use the ternary expression:

# Syntax: value_if_true if condition else value_if_false
status = "adult" if age >= 18 else "minor"
label = "even" if x % 2 == 0 else "odd"

Avoid nesting ternaries — they become unreadable quickly.

Chained Comparisons

Python supports chaining comparison operators in a way that reads naturally:

# Instead of: if x > 0 and x < 100:
if 0 < x < 100:
    print("x is between 0 and 100")

if 1 <= score <= 10:
    print("Valid score")

Nested Conditions

user_role = "admin"
is_active = True

if user_role == "admin":
    if is_active:
        print("Active admin — full access")
    else:
        print("Inactive admin — read only")
else:
    print("Regular user")

For deeply nested conditionals, consider combining conditions with and/or to flatten the logic.

Pattern Matching with match (Python 3.10+)

For matching against specific values or structures, Python 3.10 introduced match/case:

command = "quit"

match command:
    case "quit":
        print("Exiting")
    case "help":
        print("Showing help")
    case "start" | "run":
        print("Starting")
    case _:
        print(f"Unknown command: {command}")

match is not just a switch statement — it supports destructuring, guards, and matching against data structures. It's the right tool when you're branching on the shape or value of structured data.

Conclusion

For most decision logic, if/elif/else is all you need. Use ternary expressions to simplify single-line assignments, chained comparisons for range checks, and match/case when you're dispatching on specific values or data shapes. Always prefer flat, readable conditions over deeply nested blocks.