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.
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:
Example:
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.
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:
These are concise without being cryptic.
It also works inside return statements:
Comparison with a Normal if Statement
This:
is equivalent to:
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:
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:
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
ifstatement is usually more readable.
Related reading
- Does anyone know of a asynchronous mysql lib for python?
- Does Conda replace the need for virtualenv?
- Does it make sense to use Conda Poetry?
- Does it make sense to use Conda Poetry?
- Does Kafka python API support stream processing?
- Does keras.backend.clear_session deletes sessions in a process or globally?
- Does python-requests support HTTP2 and asynchronous calls?
- Does Python have a package/module management system?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.