ternary
conditional
operator
python

Does Python have a ternary conditional operator?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

Yes. Python has a ternary conditional expression, although the syntax is different from the condition ? a : b form used in languages such as C, Java, or JavaScript. In Python, the form is value_if_true if condition else value_if_false.

The Syntax

The basic pattern is:

python
result = value_if_true if condition else value_if_false

Example:

python
age = 18
status = "adult" if age >= 18 else "minor"
print(status)

This reads almost like English: use "adult" if the condition is true, otherwise use "minor".

How Evaluation Works

Python evaluates the condition first. Then it evaluates only one of the two result expressions.

python
1def yes():
2    print("yes branch")
3    return 1
4
5
6def no():
7    print("no branch")
8    return 0
9
10
11value = yes() if 3 > 1 else no()
12print(value)

Only the true branch runs here. The false branch is not evaluated.

That behavior makes the expression useful not only for short assignments, but also for choosing between computations.

Common Use Cases

A ternary expression is most useful when the result is short and directly tied to one condition.

Examples:

python
max_value = a if a > b else b
label = "even" if n % 2 == 0 else "odd"
message = "ready" if is_ready else "waiting"

These are concise without being cryptic.

It also works inside return statements:

python
def parity(n):
    return "even" if n % 2 == 0 else "odd"

Comparison with a Normal if Statement

This:

python
result = "yes" if flag else "no"

is equivalent to:

python
1if flag:
2    result = "yes"
3else:
4    result = "no"

The statement form is longer, but sometimes clearer when the logic or expressions become more complicated.

It Works Inside Larger Expressions Too

Because this is an expression, not a statement, you can use it in places where Python expects a value:

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

That is convenient, but the same readability rule still applies: if the logic grows, move it back into normal statements.

Nested Ternary Expressions

Python allows nesting:

python
score = 85
grade = "A" if score >= 90 else "B" if score >= 80 else "C"
print(grade)

This works, but readability drops quickly. For more than one simple condition, a normal if and elif block is usually better.

Why Python Uses This Syntax

The Python syntax puts the true branch first, followed by the condition, and then the false branch. That can feel unusual if you come from C-style languages, but it was chosen to read more naturally.

Once you learn the pattern, it becomes easy to scan:

x if condition else y

Common Pitfalls

The biggest mistake is writing the C-style ternary form with ? and :. Python does not support that syntax.

Another mistake is overusing nested conditional expressions until one line becomes harder to read than a normal if block.

Developers also sometimes forget the order and try to write condition if a else b, which is invalid because the condition belongs in the middle.

Finally, do not use a ternary expression just to save vertical space. If the code is becoming harder to understand, use a regular if statement instead.

Summary

  • Python does have a ternary conditional expression.
  • The syntax is value_if_true if condition else value_if_false.
  • Only one result branch is evaluated after the condition is checked.
  • It is best for short, clear conditional assignments or return values.
  • For complex or nested logic, a normal if statement is usually more readable.

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.