Python
if-elif-else
coding best practices
code readability
programming tips

Putting an if-elif-else statement on one line?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Python has a one-line conditional expression, but it does not have a separate special syntax for a one-line if-elif-else statement. If you want multiple branches in one expression, you do it by nesting conditional expressions. That works for tiny cases, but once the logic matters, a normal multi-line block is usually clearer.

The Real One-Line Conditional Form

The standard one-line form is the conditional expression:

python
status = "adult" if age >= 18 else "minor"

This is an expression, which means it produces a value. That is why it fits naturally in assignments, return statements, and small formatting decisions.

python
def shipping_label(weight):
    return "light" if weight < 5 else "heavy"

For a two-way choice, this is idiomatic and readable.

How to Emulate elif

If you really want if-elif-else behavior on one logical line, you nest another conditional expression in the else branch.

python
def classify(score):
    return "low" if score < 40 else "medium" if score < 70 else "high"

This works, but readability drops quickly as conditions become more complex. That is the real tradeoff: Python lets you do it, but that does not mean it is usually the best way to present the logic.

Prefer a Normal Block for Real Logic

For most business logic, a normal if block is easier to read, test, and modify.

python
1def classify(score):
2    if score < 40:
3        return "low"
4    elif score < 70:
5        return "medium"
6    else:
7        return "high"

This is usually the better choice once the branches need comments, logging, or future edits.

Sometimes the Problem Is Really a Mapping

If the code is just mapping discrete values to labels, a dictionary or match statement may be clearer than nested conditional expressions.

python
1def state_label(code):
2    labels = {
3        1: "queued",
4        2: "running",
5        3: "done",
6    }
7    return labels.get(code, "unknown")

That communicates value dispatch better than compressing several comparisons into one expression.

Keep One-Line Conditionals Local and Obvious

A good one-line conditional is short, obvious, and local to a small decision. A bad one-line conditional hides important rules just to save vertical space.

That is the standard to use: optimize for understanding, not for minimum line count.

If you find yourself re-indenting the expression mentally to understand it, that is usually the signal to stop compressing it and switch back to a normal block.

Readability is not a cosmetic concern here. It directly affects how confidently someone else can modify the logic later. That matters especially in code reviews, where clever one-liners often cost more time than they save. It is also the reason style guides tend to tolerate short conditional expressions but push back on nested multi-branch compression. Once the expression stops feeling instantly obvious, the line has already become too clever for its own good.

Common Pitfalls

  • Assuming Python has a separate one-line if-elif-else statement syntax.
  • Nesting too many conditions and making the expression harder to read than a normal block.
  • Using one-line conditionals for side-effect-heavy logic instead of simple value selection.
  • Compressing business rules just to reduce line count.

Summary

  • Python supports a one-line conditional expression for value selection.
  • Multi-branch behavior is written by nesting that expression in the else branch.
  • Small two-way cases are often fine on one line.
  • Most real if-elif-else logic is clearer as a normal multi-line block.
  • Use the shortest form only when it is also the clearest form.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.