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
🌐
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.
Discussions

Conditional/ternary operator for expressions in Python - Stack Overflow
Many languages have a conditional (AKA ternary) operator. This allows you to make terse choices between two values based on a condition, which makes expressions, including assignments, concise. My... More on stackoverflow.com
🌐 stackoverflow.com
A ternary operator for typed Python - Typing - Discussions on Python.org
Hello, this is my first post here, so I’m sorry if this has already been discussed. I don’t see any PEPs related to this and my search of this forum and GitHub came back empty. Proposal would be to add the following types: If - for ternary operations Equals for comparing straight equality ... More on discuss.python.org
🌐 discuss.python.org
1
August 16, 2025
Understanding Ternary Operator in Python
I'm currently transitioning from JavaScript to Python and I'm trying to understand if Python has something similar to the ternary operator as JavaScript does. In JavaScript, I would write a ternary More on stackoverflow.com
🌐 stackoverflow.com
Python ternary operator
You'll find a wide spectrum of opinions on any matter of taste, which is what this is. There's no right or wrong answer. But I don't think it's controversial to say that it can be abused badly by people trying to be too cute and trying to cram too much into one line at the cost of readability. If the usage is natural and simple as in your example I don't see much of a problem. More on reddit.com
🌐 r/learnprogramming
6
3
April 28, 2012
Top answer
1 of 16
9350

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.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › ternary-operator-in-python
Ternary Operator in Python?
July 30, 2019 - 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
🌐
YouTube
youtube.com › mcoding
Python's ternary operator - YouTube
It's not so hidden!Yes, like nearly every modern programming language Python also has a ternary operator, although it doesn't refer to it as such. While othe...
Published   March 27, 2023
Views   39K
Find elsewhere
🌐
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.
🌐
W3Schools
w3schools.com › python › python_if_shorthand.asp
Python Shorthand If
If you have one statement for if and one for else, you can put them on the same line using a conditional expression: ... This is called a conditional expression (sometimes known as a "ternary operator").
🌐
Python Geeks
pythongeeks.org › python geeks › learn python › python ternary operator with example
Python Ternary Operator with Example - Python Geeks
June 14, 2022 - In the Python programming language, the Ternary Operator is a condition expression that allows developers to evaluate statements. The Ternary Operators perform an action based on whether the statement is True or False.
🌐
DataCamp
datacamp.com › tutorial › pythons-ternary-operators-guide
Python's Ternary Operators Guide: Boosting Code Efficiency | DataCamp
May 17, 2024 - In the following section, I will review examples that cover most combinations of conditions, result_if_True and result_if_False. Ternary operators offer a concise way to express conditional logic not only in simple if-else statements but also in other Python constructs like tuples and dictionaries.
🌐
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.
🌐
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 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".
🌐
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.
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to use ternary operator in python?
Ternary Operator in Python : A Complete Guide
May 17, 2024 - The ternary operator in Python is a powerful tool that allows you to write concise and elegant code. It’s a conditional expression that evaluates a condition and returns one of two values based on whether it is true or false.
🌐
The Teclado Blog
blog.teclado.com › python-pythons-ternary-operator
Python's "Ternary Operator"
October 26, 2022 - Conditional expressions are a slightly obscure bit of syntax in Python, but they essentially allow us to assign values to variables based on some condition. ... In this case we have some value bound to the variable x, and we check if the value of x is less than 10. If it is, we assign the number to value; otherwise, we assign the string, "Invalid value". We can see a case where the value of x is not less than 10 below: x = 10 value = x if x < 10 else "Invalid value" # Invalid value
🌐
Python.org
discuss.python.org › typing
A ternary operator for typed Python - Typing - Discussions on Python.org
August 16, 2025 - Hello, this is my first post here, so I’m sorry if this has already been discussed. I don’t see any PEPs related to this and my search of this forum and GitHub came back empty. Proposal would be to add the following types: If - for ternary operations Equals for comparing straight equality ==. Seems like pyright does this but mypy doesn’t. IsInstance to see if an object is a type.