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:
- As an expression:
result = "yes" if condition else "no"— this is usually what people mean by “ternary” in Python. - 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):
value_if_true if condition else value_if_falsePython 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
score = 85
label = "pass" if score >= 60 else "fail"
print(label)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:
theme = "night"
mode = "dark" if theme == "night" else "light"
print(mode)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:
age = 20
if age >= 18:
print("adult")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:
x = 1
if x > 0:
print("positive")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.
number = 0
result = "negative" if number < 0 else "positive" if number > 0 else "zero"
print(result)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:
credits = 750
tier = "gold" if credits > 1000 else ("silver" if credits > 500 else "bronze")
print(tier)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.
numbers = [1, 2, 3, 4]
labels = ["even" if n % 2 == 0 else "odd" for n in numbers]
print(labels)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:
n = 7
parity = "even" if n % 2 == 0 else "odd"
print(parity)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:
score = 72
letter = "A" if score >= 90 else "B" if score >= 80 else "C"
print(letter)score = 72
if score >= 90:
letter = "A"
elif score >= 80:
letter = "B"
else:
letter = "C"
print(letter)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:
safe = 0 if True else 1 / 0
print(safe)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:
x = 1
label = "a" if x > 2 else "b" if x > 1 else "c" if x > 0 else "d"
print(label)x = 1
if x > 2:
label = "a"
elif x > 1:
label = "b"
elif x > 0:
label = "c"
else:
label = "d"
print(label)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):
x = 5
if x > 0:
print("positive")Expression form (produces a value you can assign):
x = 5
message = "positive" if x > 0 else "non-positive"
print(message)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:
x = 1
print("a") if x > 0 else print("b")Prefer:
x = 1
if x > 0:
print("a")
else:
print("b")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.

