Yes, it was added in version 2.5. The expression syntax is:

a if condition else b

First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

For example:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Note that conditionals are an expression, not a statement. This means you can't use statements such as pass, or assignments with = (or "augmented" assignments like +=), within a conditional expression:

>>> pass if False else pass
  File "<stdin>", line 1
    pass if False else pass
         ^
SyntaxError: invalid syntax

>>> # Python parses this as `x = (1 if False else y) = 2`
>>> # The `(1 if False else x)` part is actually valid, but
>>> # it can't be on the left-hand side of `=`.
>>> x = 1 if False else y = 2
  File "<stdin>", line 1
SyntaxError: cannot assign to conditional expression

>>> # If we parenthesize it instead...
>>> (x = 1) if False else (y = 2)
  File "<stdin>", line 1
    (x = 1) if False else (y = 2)
       ^
SyntaxError: invalid syntax

(In 3.8 and above, the := "walrus" operator allows simple assignment of values as an expression, which is then compatible with this syntax. But please don't write code like that; it will quickly become very difficult to understand.)

Similarly, because it is an expression, the else part is mandatory:

# Invalid syntax: we didn't specify what the value should be if the 
# condition isn't met. It doesn't matter if we can verify that
# ahead of time.
a if True

You can, however, use conditional expressions to assign a variable like so:

x = a if True else b

Or for example to return a value:

# Of course we should just use the standard library `max`;
# this is just for demonstration purposes.
def my_max(a, b):
    return a if a > b else b

Think of the conditional expression as switching between two values. We can use it when we are in a 'one value or another' situation, where we will do the same thing with the result, regardless of whether the condition is met. We use the expression to compute the value, and then do something with it. If you need to do something different depending on the condition, then use a normal if statement instead.


Keep in mind that it's frowned upon by some Pythonistas for several reasons:

  • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order).
  • Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
  • Stylistic reasons. (Although the 'inline if' can be really useful, and make your script more concise, it really does complicate your code)

If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.

Official documentation:

  • Conditional expressions
  • Is there an equivalent of C’s ”?:” ternary operator?
Answer from Vinko Vrsalovic on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › ternary-operator-in-python
Ternary Operator in Python - GeeksforGeeks
The ternary operator in Python perform conditional checks and assign values or execute expressions in a single line.
Published   December 20, 2025
Top answer
1 of 16
9351

Yes, it was added in version 2.5. The expression syntax is:

a if condition else b

First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

For example:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Note that conditionals are an expression, not a statement. This means you can't use statements such as pass, or assignments with = (or "augmented" assignments like +=), within a conditional expression:

>>> pass if False else pass
  File "<stdin>", line 1
    pass if False else pass
         ^
SyntaxError: invalid syntax

>>> # Python parses this as `x = (1 if False else y) = 2`
>>> # The `(1 if False else x)` part is actually valid, but
>>> # it can't be on the left-hand side of `=`.
>>> x = 1 if False else y = 2
  File "<stdin>", line 1
SyntaxError: cannot assign to conditional expression

>>> # If we parenthesize it instead...
>>> (x = 1) if False else (y = 2)
  File "<stdin>", line 1
    (x = 1) if False else (y = 2)
       ^
SyntaxError: invalid syntax

(In 3.8 and above, the := "walrus" operator allows simple assignment of values as an expression, which is then compatible with this syntax. But please don't write code like that; it will quickly become very difficult to understand.)

Similarly, because it is an expression, the else part is mandatory:

# Invalid syntax: we didn't specify what the value should be if the 
# condition isn't met. It doesn't matter if we can verify that
# ahead of time.
a if True

You can, however, use conditional expressions to assign a variable like so:

x = a if True else b

Or for example to return a value:

# Of course we should just use the standard library `max`;
# this is just for demonstration purposes.
def my_max(a, b):
    return a if a > b else b

Think of the conditional expression as switching between two values. We can use it when we are in a 'one value or another' situation, where we will do the same thing with the result, regardless of whether the condition is met. We use the expression to compute the value, and then do something with it. If you need to do something different depending on the condition, then use a normal if statement instead.


Keep in mind that it's frowned upon by some Pythonistas for several reasons:

  • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order).
  • Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
  • Stylistic reasons. (Although the 'inline if' can be really useful, and make your script more concise, it really does complicate your code)

If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.

Official documentation:

  • Conditional expressions
  • Is there an equivalent of C’s ”?:” ternary operator?
2 of 16
1028

You can index into a tuple:

(falseValue, trueValue)[test]

test needs to return True or False.
It might be safer to always implement it as:

(falseValue, trueValue)[test == True]

or you can use the built-in bool() to assure a Boolean value:

(falseValue, trueValue)[bool(<expression>)]
🌐
Python Tips
book.pythontips.com › en › latest › ternary_operators.html
6. Ternary Operators — Python Tips 0.1 documentation
I just released the alpha version of my new book; Practical Python Projects. Learn more about it on my blog. In 325+ pages, I will teach you how to implement 12 end-to-end projects. You can buy it from Feldroy.com. ... Ternary operators are more commonly known as conditional expressions in Python.
🌐
Mimo
mimo.org › glossary › python › ternary-operator
Python Ternary Operator: Syntax, Usage, and Examples
Just be cautious—nested ternary operators can quickly become difficult to read. ... numbers = [1, 2, 3, 4] length_status = "Long list" if len(numbers) > 3 else "Short list" print(length_status) This pattern is especially useful in data validation and summaries. Python code that processes collections often benefits from using ternary operator techniques for concise transformations.
🌐
DataCamp
datacamp.com › tutorial › pythons-ternary-operators-guide
Python's Ternary Operators Guide: Boosting Code Efficiency | DataCamp
May 17, 2024 - In the context of tuples, the ternary operator selects one of two values based on a condition and returns it. This is achieved by indexing the tuple using the result of the condition as the index. If the condition is True, in Python True =1, the element at index 1 (corresponding to result_if_true) is returned; otherwise, the element at index 0 (corresponding to result_if_false) is returned.
🌐
Dataquest
dataquest.io › blog › python-ternary-operator
Python Ternary: How to Use It and Why It's Useful (with Examples)
March 6, 2023 - The Python ternary operator (or conditional operator), tests if a condition is true or false and, depending on the outcome, returns the corresponding value — all in just one line of code.
🌐
freeCodeCamp
freecodecamp.org › news › python-tenary-operator
Python Ternary Operator – Conditional Operators in Python
April 26, 2023 - The ternary operator in Python is simply a shorter way of writing an if and if...else statements.
🌐
Codecademy
codecademy.com › article › python-ternary-operator
Guide to Using Ternary Operator in Python | Codecademy
The Python ternary operator is an efficient way to make quick conditional decisions based on a single expression. In this article, we will discuss how the ternary operator works in Python compared to if-else statements, its advantages and disadvantages, and the best practices for using it.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › ternary-operator-in-python
Ternary Operator in Python?
Many programming languages support ternary operator, which basically define a conditional expression. Similarly the ternary operator in python is used to return a value based on the result of a binary condition. It takes binary value(conditi
🌐
Scaler
scaler.com › home › topics › python › ternary operator in python
Ternary Operator in Python - Scaler Topics
April 21, 2023 - The ternary operator in Python is a concise way of writing simple if/else statements in a single line. It returns a true or false value by evaluating a boolean condition. It is shorter and more readable than simple if/else statements.
🌐
Python Morsels
pythonmorsels.com › ternary-operator
Python's ternary operator - Python Morsels
November 14, 2022 - Python doesn't have the traditional ternary operator that most programming languages do. Instead we have "conditional expressions".
🌐
Wiingy
wiingy.com › home › learn › python › ternary operators in python
Ternary Operators in Python
January 30, 2025 - The ternary operator in Python is a shortcut for building a straightforward if-else statement. It is also known as a conditional expression, and it enables programmers to create code that is clear and legible.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python ternary operator
Python Ternary Operator
March 26, 2025 - value_if_true if condition else value_if_falseCode language: Python (python) The ternary operator evaluates the condition. If the result is True, it returns the value_if_true. Otherwise, it returns the value_if_false. The ternary operator is equivalent to the following if...else statement:
🌐
DataFlair
data-flair.training › blogs › python-ternary-operator
Python Ternary Operator - 5 Ways To Implement Ternary Operators - DataFlair
July 14, 2025 - Python packs a single-line “choose this or that” known as the ternary operator, or conditional expression. The form is value_if_true if condition else value_if_false. It swaps bulky multi-line if-else blocks for swift inline decisions.
🌐
DEV Community
dev.to › askyt › python-ternary-operator-a-comprehensive-guide-274i
Python Ternary Operator: A Comprehensive Guide - DEV Community
December 14, 2024 - The ternary operator, also known as the conditional expression, is a concise way to execute simple if-else logic. It was introduced in Python 2.5 and has become a widely used tool for improving code readability and compactness.
🌐
WsCube Tech
wscubetech.com › resources › python › ternary-operator
Ternary Operator in Python (Conditional Operator With Example)
October 1, 2025 - Learn how to use the Ternary Operator in Python, also known as the Conditional Operator, for concise one-line if-else statements in your Python code.
🌐
Sentry
sentry.io › sentry answers › python › does python have a ternary conditional operator?
Does Python Have a Ternary Conditional Operator? | Sentry
If so, what is the structure of a Python ternary operator? The ternary operator is used to shorten the code needed to write if-else blocks. The syntax of ternary operators naturally differs between languages, but in most cases, the operator ...
🌐
Flexiple
flexiple.com › python › python-ternary
Python ternary operators - How to use them? - Flexiple Tutorials - Flexiple
The ternary operator evaluates the condition which is if int(your_age) >= 18. If the result is true, then it returns the val_true, which in this case is “Yes, you can drive!”. Else it returns val_false, which is Sorry, you can’t drive yet!” · As we now have an understanding of how Python’s ternary operator works, let’s see how it applies with Tuples, Dictionary and Lambda.