value = b if a > 10 else c

For Python 2.4 and lower you would have to do something like the following, although the semantics isn't identical as the short circuiting effect is lost:

value = [c, b][a > 10]

There's also another hack using 'and ... or' but it's best to not use it as it has an undesirable behaviour in some situations that can lead to a hard to find bug. I won't even write the hack here as I think it's best not to use it, but you can read about it on Wikipedia if you want.

Answer from Mark Byers on Stack Overflow
๐ŸŒ
Python Tips
book.pythontips.com โ€บ en โ€บ latest โ€บ ternary_operators.html
6. Ternary Operators โ€” Python Tips 0.1 documentation
The above example is not widely used and is generally disliked by Pythonistas for not being Pythonic. It is also easy to confuse where to put the true value and where to put the false value in the tuple. Another reason to avoid using a tupled ternery is that it results in both elements of the tuple being evaluated, whereas the if-else ternary operator does not.
๐ŸŒ
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
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Using the question mark (?) for inline conditions - Python Help - Discussions on Python.org
August 6, 2024 - In JavaScript, you can code some conditional assignment by using โ€œ?โ€ var weather = temperature > 30 ? โ€œHotโ€ : โ€œcoldโ€ It means that weather will be set to โ€˜hotโ€™ if temperature > 30 is true, and to โ€˜coldโ€™ if temperature > 30 is false. You can find more here.
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ python-ternary-operator
Python Ternary Operator
However, as we will see, Python does things a bit differently. In Python, there is no question mark and colon syntax for ternary operation. So in that sense you can say there is not one single "ternary operator".
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ does-python-have-a-ternary-conditional-operator-830454c34de7
Does Python Have a Ternary Conditional Operator? | by Jonathan ...
October 14, 2019 - The ternary operator is traditionally expressed using the question mark and colon organized as expression ? positive value : negative value ยท When the expression evaluates to true, the positive value is usedโ€”otherwise the negative value is used.
๐ŸŒ
Apenwarr
apenwarr.ca โ€บ log โ€บ 20090320
The ternary operator in python - apenwarr
So as I was saying, the question ... But for the love of God, don't. The point of the ternary operator is it checks the truthiness of the first parameter, and if it's true, returns the second parameter....
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>)]
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. In other words, it's a compact alternative to the common multiline if-else control flow statements in situations when we only need to "switch" between two values.
๐ŸŒ
ITNEXT
itnext.io โ€บ hacking-the-python-syntax-part-1-ternary-operator-bbcb04aa6ecb
Hacking the Python syntax: Ternary operator | by Raimi Karim | ITNEXT
December 31, 2022 - Here are some examples of tokens: ... and 'hello!' ... The ternary operator has 2 symbols: ? and :. The : is a token value for COLON but the question mark ......
๐ŸŒ
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".
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ does python have a ternary conditional operator?
Does Python Have a Ternary Conditional Operator? | Sentry
The ternary operator above checks if x is greater than y, and if it is, assigns the value of x (10) to the variable z. If youโ€™re looking for information on Python application monitoring, check out these links from our blog:
๐ŸŒ
Python
peps.python.org โ€บ pep-0308
PEP 308 โ€“ Conditional Expressions | peps.python.org
Supporters of a cond() function point out that the need for short-circuit evaluation is rare. Scanning through existing code directories, they found that if/else did not occur often; and of those only a few contained expressions that could be helped by cond() or a ternary operator; and that most of those had no need for short-circuit evaluation.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-tenary-operator
Python Ternary Operator โ€“ Conditional Operators in Python
April 26, 2023 - Here's what an if...else statement looks like in Python: user_score = 90 if user_score > 50: print("Next level") else: print("Repeat level") In the code above, we created a variable user_score with a value of 90. We then printed either of two statements based on a predefined condition โ€” if user_score > 50. So if the user_score variable is greater than 50, we print "Next level". If it's less than user_score, we print "Repeat level". You can shorten the if...else statement using the ternary operator syntax.
๐ŸŒ
i2tutorials
i2tutorials.com โ€บ home โ€บ blogs โ€บ ternary conditional operator in python
Ternary Conditional Operator in Python | i2tutorials | Ternary Conditional Operator |
March 25, 2022 - The ternary operator has three components: expression, positive value and negative value. In the standardized way of expressing the ternary operator, we use a question mark and a colon.
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-ternary-conditional-operator
Does Python Have a Ternary Conditional Operator? | LearnPython.com
The syntax for a Python ternary conditional operator is: ... This code evaluates the condition. If it's true, the code returns the value of value_if_true; if it's false, it returns the value of value_if_false. It's important to note that the if and else are mandatory. Let's look at an example to see how the Python conditional expression works:
๐ŸŒ
Quora
quora.com โ€บ Why-should-you-use-a-Python-ternary-operator-instead-of-an-if-else-statement
Why should you use a Python ternary operator instead of an if-else statement? - Quora
Answer: Why should you use a Python ternary operator instead of an if-else statement? Should you? In general, not just in python. And as a rule of thumb, pythonโ€™s ternary looks different than Cโ€™s, and PHPโ€™s, so if you ask me, the exit is right where you came from, so please leave the room with t...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-ternary-operator-in-python
What is the ternary operator in Python?
The ternary operator can be thought of as a simplified, one-line version of the if-else statement to test a condition. ... Letโ€™s write a Python code snippet to check if an integer is even or odd.
๐ŸŒ
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.