Python One Line If Else

Learn how to write Python if else in one line using the conditional expression syntax. Includes one-line if, if else, if elif else, nested conditions, and common mistakes.

Published

Updated

Read time 7 min read

Reviewed byDeepak Prasad

Python One Line If Else

You often want either a value picked by a condition (the conditional expression, sometimes called a ternary form) or a tiny guard that fits on one line. This guide separates those ideas, shows the if ... else ... expression syntax, covers chained “elif-like” logic, one-line if statements, comprehensions, and when to stop compressing and use a normal if / else block. For the dedicated ternary-operator walkthrough, see Python ternary operator; for list comprehensions with filters, see list comprehension examples.

The language reference defines the conditional expression; compound statements describe multi-line if, elif, and else—including the fact that simple forms can appear on one line.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Can you write if else in one line in Python?

Yes, in two different senses:

  1. As an expression: result = "yes" if condition else "no" — this is usually what people mean by “ternary” in Python.
  2. As a compact statement: if condition: print("yes") — valid for a single simple statement, but not a substitute for readable multi-line control flow.

Pick the form that matches whether you need a value or a side effect.


Python one-line if else syntax

Conditional expression (ternary-style):

text
value_if_true if condition else value_if_false

Python evaluates condition first. If it is true, value_if_true is evaluated and becomes the result; otherwise value_if_false is evaluated. The branch not taken is skipped (short-circuit), unlike misreadings that talk about “evaluating else first.”


Python one-line if else example

python
score = 85
label = "pass" if score >= 60 else "fail"
print(label)
Output

You should see pass. The same logic as a four-line if / else that assigns label, without a temporary block.


Assign value using one-line if else

Assignment is the most common use:

python
theme = "night"
mode = "dark" if theme == "night" else "light"
print(mode)
Output

That prints dark. For broader branching patterns, keep reading the chained form in the next section or fall back to a full block.


Python one-line if without else

When there is no else, you are using a normal if statement with its suite on the same line:

python
age = 20
if age >= 18:
    print("adult")
Output

That is not the conditional-expression syntax (there is no else, so you cannot write x if c else y). It is fine for tiny demos; anything with more than one short step belongs in a multi-line block for readability.


Python if statement in one line

The grammar allows a simple if (or if / elif / else) suite to sit on the same line as the header when the suite is a single simple statement. You can also separate simple statements with semicolons on one line, but that scales poorly:

python
x = 1
if x > 0:
    print("positive")
Output

You should see positive. The reference notes that compound statements are usually written across several lines; one-line forms are restricted to simple statements, not arbitrary nested logic.


Python if elif else in one line

Python has no elif keyword inside a conditional expression. Instead, you chain expressions: a if c1 else b if c2 else d groups as a if c1 else (b if c2 else d). Use this only when the conditions stay short and obvious.

python
number = 0
result = "negative" if number < 0 else "positive" if number > 0 else "zero"
print(result)
Output

That prints zero. A real if / elif / else block is still clearer when branches grow or need side effects—expressions cannot carry elif control flow directly.


Nested if else in one line

Parentheses keep nested conditional expressions readable:

python
credits = 750
tier = "gold" if credits > 1000 else ("silver" if credits > 500 else "bronze")
print(tier)
Output

That prints silver. If nesting deepens, switch to a helper function or a standard if ladder.


One-line if else inside list comprehension

Inside comprehensions, the conditional-expression form filters or maps per element. That pairs naturally with loops written in one expression; see Python for loop in one line for the surrounding comprehension patterns.

python
numbers = [1, 2, 3, 4]
labels = ["even" if n % 2 == 0 else "odd" for n in numbers]
print(labels)
Output

You should see ['odd', 'even', 'odd', 'even'].


One-line if else vs normal if else

Use a conditional expression when you assign or return a single value from a simple binary choice. Use a normal if / elif / else block when you need multiple statements, clearer stepping, or elif that is not just a chain of expressions. Readability beats line count once the condition or branches stop fitting comfortably in one short line.

Binary choice, one value—an expression reads well:

python
n = 7
parity = "even" if n % 2 == 0 else "odd"
print(parity)
Output

You should see odd. Several ordered thresholds are still legal as a chain, but a block often scans faster once you add comments, logging, or more than one action:

python
score = 72
letter = "A" if score >= 90 else "B" if score >= 80 else "C"
print(letter)
Output
python
score = 72
if score >= 90:
    letter = "A"
elif score >= 80:
    letter = "B"
else:
    letter = "C"
print(letter)
Output

Both versions print C; the second leaves room to insert extra lines under each branch without turning the condition into a puzzle.


Common mistakes with Python one-line if else

Thinking else runs first

Python evaluates the condition, then only the chosen branch. If the “false” branch ran eagerly, the line below would crash on division by zero:

python
safe = 0 if True else 1 / 0
print(safe)
Output

You should see 0.

Over-long chains

Deep else ... if ... chains mirror elif logic but hide the order. Compare a terse chain with an explicit block for the same x = 1 case:

python
x = 1
label = "a" if x > 2 else "b" if x > 1 else "c" if x > 0 else "d"
print(label)
Output
python
x = 1
if x > 2:
    label = "a"
elif x > 1:
    label = "b"
elif x > 0:
    label = "c"
else:
    label = "d"
print(label)
Output

Both print c; the block states the ladder in the same order you would read an elif stack.

Putting statements where only expressions belong

Each side of if ... else ... must be an expression. You cannot drop a full if suite or arbitrary statements in those slots—use a normal block instead.

Mixing up a one-line if statement with the ternary expression

Statement form (suite after the colon, no value produced):

python
x = 5
if x > 0:
    print("positive")
Output

Expression form (produces a value you can assign):

python
x = 5
message = "positive" if x > 0 else "non-positive"
print(message)
Output

The first prints positive directly; the second assigns then you print the string.

Using the expression only for side effects

This works but reads backwards compared to a block:

python
x = 1
print("a") if x > 0 else print("b")
Output

Prefer:

python
x = 1
if x > 0:
    print("a")
else:
    print("b")
Output

Expecting elif inside an expression

There is no elif token in expressions—only chained else ( ... if ... ). When you want real elif, use the compound statement form shown earlier in this article.


Python one-line if else quick reference table

Goal Pattern Note
Pick between two values a if cond else b Expression only
Many branches chain ... else ... if ... Not real elif; keep short
One-line guard, no else if cond: stmt Single simple statement
Transform in comprehension f(x) if cond else g(x) for x in xs Often clearer than deep nesting
Heavy logic multi-line if / elif / else Preferred for maintenance

Summary

The idiomatic one-line if / else in Python is the conditional expression: value_if_true if condition else value_if_false, which evaluates the condition first and only runs the chosen branch. One-line if statements cover tiny guards without else. There is no inline elif token—only chained expressions—so prefer normal multi-line if / elif / else blocks when the decision tree grows. Comprehensions combine naturally with inline conditionals; the list-comprehension section above links to the companion one-line loop guide when you need the full comprehension pattern.


References


Frequently Asked Questions

1. What is the syntax for a one-line if else in Python?

Use a conditional expression: value_if_true if condition else value_if_false; only the chosen branch is evaluated.

2. Is a one-line if with print the same as the ternary operator?

No—a one-line if statement runs statements after the colon; the conditional expression produces a value and cannot replace arbitrary statements without extra tricks.

3. Can I write true elif on a single line?

There is no elif token inside an expression; you chain conditional expressions instead, which is not the same control-flow structure as a multi-line if/elif/else.

4. When should I avoid chaining many inline conditions?

When readers cannot see the decision order at a glance—switch to a normal if/elif/else block or a helper function.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …