Technical skill practice

If Else in Python: Order Conditions and Check Boundaries

Python if/elif/else chooses the first true branch in an ordered chain. Separate if statements are independent and can both run. When rules overlap, their order and boundary operators determine the result; a visually similar collection of conditions can implement a different policy.

Start with a small, explicit task

Route synthetic nonnegative whole-unit expense amounts: zero needs no approval, values through 50 use a desk review, and larger values use a manager review. Reject negatives. These thresholds are invented for the exercise and do not represent an employer’s expense policy.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_if_else.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

['invalid', 'none', 'desk', 'desk', 'manager']

['positive', 'within desk limit']

def route(amount):
    if amount < 0:
        return "invalid"
    elif amount == 0:
        return "none"
    elif amount <= 50:
        return "desk"
    else:
        return "manager"
values = [-1, 0, 1, 50, 51]
results = [route(value) for value in values]
assert results == ["invalid", "none", "desk", "desk", "manager"]
independent = []
if 50 > 0:
    independent.append("positive")
if 50 <= 50:
    independent.append("within desk limit")
assert len(independent) == 2
print(results)
print(independent)

Explain the result

The zero branch precedes the <= 50 branch because zero also satisfies that broader comparison. Placing the broader test first would hide the special zero case. The values 50 and 51 check the exact transition between desk and manager, while -1 and 0 check invalid and special-case behavior.

The independent conditions both run for 50, producing two labels. That can be correct when collecting several properties, but it is different from choosing one routing destination. Decide whether the task needs one exclusive result or several independently true facts before choosing the structure.

Avoid these mistakes

Do not use = when you mean equality comparison ==. Preserve indentation so each block belongs to the intended condition. Add explicit boundary examples whenever changing < to <=, and avoid relying only on an ordinary middle value that would pass either version.

Questions, answered

Can more than one elif branch run?

A single if/elif/else chain takes its first matching branch. Separate if statements can each run.

Why test 50 and 51?

They sit on opposite sides of the stated review threshold, exposing off-by-one comparison mistakes.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.