If, Elif, and Else in Python, How Programs Make Decisions

Last edited Oct 12, 2026 · 4 min read

Programs become useful the moment they can choose: charge shipping or not, warn the user or carry on, file a message here or there. In Python, choices are written with if, elif and else. The syntax is short, but a few details (order, indentation, and what counts as "true") decide whether your program does what you meant. Every example below was run in Python 3.13, and the output shown is what it printed.

1. The basic shape

The official Python tutorial introduces the if statement with this structure: one if, zero or more elif parts, and an optional else. It also notes that elif is short for "else if" and that an if ... elif ... elif sequence stands in for the switch or case statements found in other languages.

Here is a shipping rule for an online store: free shipping at 50 dollars or more, 4.99 from 25 dollars, and 7.99 below that.

def shipping_cost(total):
    if total >= 50:
        return 0
    elif total >= 25:
        return 4.99
    else:
        return 7.99

for t in [60, 30, 10, 50, 25]:
    print(t, shipping_cost(t))

Output:

60 0
30 4.99
10 7.99
50 0
25 4.99

Python checks the conditions from top to bottom and runs the block under the first one that is true. Then it skips the rest. The else block runs only when nothing above it was true.

The boundaries matter. >= means "greater than or equal to," so a total of exactly 50 gets free shipping and exactly 25 pays 4.99. The introduction chapter lists the comparison operators: <, >, ==, <=, >= and != (not equal).

2. Why the order of conditions matters

Because Python stops at the first true condition, putting a broad test above a narrow one hides the narrow one forever. Swap the first two tests:

def shipping_cost_wrong(total):
    if total >= 25:
        return 4.99
    elif total >= 50:
        return 0
    else:
        return 7.99

print(shipping_cost_wrong(60))

Output:

4.99

A 60 dollar order is also at least 25, so the first branch wins and the free shipping branch can never run. When conditions overlap, put the most specific one first.

3. Two errors you will meet early

Using = instead of ==. A single equals sign assigns a value; a double one compares. Writing if x = 5: is a syntax error, and recent Python versions say so helpfully:

SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

The tutorial points out that Python requires assignment inside expressions to be explicit precisely to avoid the classic bug of typing = when == was intended.

Forgetting to indent. The code under if must be indented. The introduction chapter explains that indentation is how Python groups statements, and that each line in a block must be indented by the same amount. Without it you get:

IndentationError: expected an indented block after 'if' statement on line 2

4. Combining conditions with and, or, not

The tutorial's "More on Conditions" section describes how comparisons can be combined with and and or, and negated with not. It also notes that not binds most tightly and or least, so A and not B or C means (A and (not B)) or C. Use parentheses whenever you are unsure.

Comparisons can also be chained, which reads like math:

age = 30
print(18 <= age < 65)

Output:

True

The in operator tests membership, which is often cleaner than a chain of or tests:

status = "pending"
if status in ("pending", "on hold"):
    print("Needs attention")

Output:

Needs attention

5. What counts as true

You do not always need a comparison. The introduction chapter explains that any non-zero number is true and zero is false, and that for strings and lists, anything with a non-zero length is true while empty sequences are false. That makes "is there anything here?" checks short:

name = ""
if name:
    print("Hello,", name)
else:
    print("No name entered")

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

Output:

No name entered
Cart is empty

An empty string and an empty list both behave as false, so the else branch and the not items branch run.

The tutorial adds one more detail worth knowing: and and or are short-circuit operators. They evaluate from left to right and stop as soon as the result is known. In A and B, if A is false, B is never even looked at.

6. When you have many fixed values

If you are comparing one value against a long list of constants, the tutorial mentions the match statement as an alternative. For a beginner, a clear if ... elif chain is fine; reach for match later when the chain gets long.

7. Key takeaways

  • An if can have any number of elif parts and an optional else; Python runs only the first true branch.
  • Order overlapping conditions from most specific to most general.
  • Use == to compare and = to assign; mixing them up is a syntax error.
  • Indent every line inside the block by the same amount.
  • and, or, not, chained comparisons and in let you express real rules clearly.
  • Zero, empty strings and empty lists count as false, so if name: checks for a non-empty value.

Sources

  1. Python Software Foundation, The Python Tutorial, More Control Flow Tools
  2. Python Software Foundation, The Python Tutorial, Data Structures (More on Conditions)
  3. Python Software Foundation, The Python Tutorial, An Informal Introduction to Python
CategoriesCoding From Zeropythonprogramming basicsconditions