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>)]
Discussions

People using Ternary expressions.....who hurt you?
If your statement makes more sense in one line then ternary would be preferred. Let's say you have to check some variables that may have an impact on some other ones. You write your assignment and you use a ternary operator. It takes one line and it is easy to understand and read because it begins with an assignment. They are great tool for some specific cases but it's easy to overuse it. More on reddit.com
🌐 r/Python
41
0
September 24, 2021
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
The ternary operator in Python

Downvoted for the ugly twitter thingy on the left obstructing view of article.

More on reddit.com
🌐 r/programming
8
4
March 10, 2006
Ternary/Conditional Operator | Is it a good practice?
it's a style thing but we use them pretty prolifically. your teacher saying it makes your code unreadable is an ignorant overstatement, but using ternaries can make code pretty unreadable if you're not careful (especially if you start nesting them). so, just keep that in mind. some people tend to get crafty and start writing stuff that looks like this: var blah = new SomeType { SomeProp = some really long comparison thing ? new SomeOtherType { SomeProp = some other long comparison thing ? another long comparison thing ? "some value" : "some other value" : yet another comparison thing ? "value" : "other value" } : new AnotherType { SomeProp = some other long comparison thing ? another long comparison thing ? "1value" : "2other value" : yet another comparison thing ? "1value" : "2value" } } which is less than ideal. More on reddit.com
🌐 r/AskProgramming
17
11
August 25, 2018
People also ask

Are Python ternary operators readable?
Ternary operators make Python code for simple conditions more readable and easily understood. However, a ternary operator can make code difficult to read if you use it for excessively nested expressions.
🌐
wscubetech.com
wscubetech.com › resources › python › ternary-operator
Ternary Operator in Python (Conditional Operator With Example)
When should we avoid using ternary operators in Python?
You should avoid using Python ternary operators when your condition is complex, involves multiple expressions, or is deeply nested, as it makes the code confusing and hard to maintain.
🌐
wscubetech.com
wscubetech.com › resources › python › ternary-operator
Ternary Operator in Python (Conditional Operator With Example)
Can I nest ternary operators in Python?
Yes, you can nest ternary expressions in Python. This allows you to check multiple conditions in one line, but be careful, it can make your code harder to read if overused.
🌐
wscubetech.com
wscubetech.com › resources › python › ternary-operator
Ternary Operator in Python (Conditional Operator With Example)
🌐
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.
🌐
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.
Find elsewhere
🌐
Apenwarr
apenwarr.ca › log › 20090320
The ternary operator in python - apenwarr
March 20, 2009 - 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....
🌐
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.
🌐
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_conditions.asp
Python If Statement
Boolean variables can be used directly in if statements without comparison operators. ... Python can evaluate many types of values as True or False in an if statement.
🌐
W3Schools
w3schools.com › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... There is also a short-hand if...else, known as the ternary operator because it uses three operands.
🌐
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.
🌐
Devcamp
bottega.devcamp.com › full-stack-development-javascript-python › guide › how-to-use-ternary-operator-python-conditionals
How to Use the Ternary Operator in Python Conditionals
So when that user logs in and we check to see their credentials if it says that their role is admin then we want them to access the dashboard. And if not, say they're a guest user then we do not. So what I'm going to do is I'm going to create another variable here called auth and then this is where we're going to store the output of our ternary operator and so I'm going to add a space and equals because we're performing an assignment and I'm going to say it can access if role is equal to admin else cannot access and that is the ternary operator syntax.
🌐
LearnPython.com
learnpython.com › blog › python-ternary-conditional-operator
Does Python Have a Ternary Conditional Operator? | LearnPython.com
The answer is yes; it is called a conditional expression rather than a ternary operator. Indeed, Python does have a ternary conditional operator in the form of a conditional expression using if and else in a single line.
🌐
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...
🌐
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").
🌐
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.
🌐
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.
🌐
Better Programming
betterprogramming.pub › does-python-have-a-ternary-conditional-operator-830454c34de7
Does Python Have a Ternary Conditional Operator?
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. Python does not follow the same syntax as previous languages; however, the technique does exist. In Python, the components are reorganized and the keywords if and else are used, which reads positive value if expression else negative value
🌐
Python documentation
docs.python.org › 3 › reference › expressions.html
6. Expressions — Python 3.14.3 documentation
1 week ago - Added in version 3.8: See PEP 572 ... A conditional expression (sometimes called a “ternary operator”) is an alternative to the if-else statement....