Does Python have a ternary conditional operator?
Asked Answered
S

32

7915

Is there a ternary conditional operator in Python?

Siam answered 27/12, 2008 at 8:32 Comment(10)
Though Pythons older than 2.5 are slowly drifting to history, here is a list of old pre-2.5 ternary operator tricks: "Python Idioms", search for the text 'Conditional expression' . Wikipedia is also quite helpful Ж:-)Gentility
In the Python 3.0 official documentation referenced in a comment above, this is referred to as "conditional_expressions" and is very cryptically defined. That documentation doesn't even include the term "ternary", so you would be hard-pressed to find it via Google unless you knew exactly what to look for. The version 2 documentation is somewhat more helpful and includes a link to "PEP 308", which includes a lot of interesting historical context related to this question.Sharyl
"ternary" (having three inputs) is a consequential property of this impelmentation, not a defining property of the concept. eg: SQL has case [...] { when ... then ...} [ else ... ] end for a similar effect but not at all ternary.Anatomy
also ISO/IEC 9899 (the C programming language standard) section 6.5.15 calls it the "the condtitional operator"Anatomy
Wikipedia covers this thoroughly in the article "?:".Decompensation
It is mentioned here docs.python.org/3/faq/…, but not mentioned in Python Standard LibraryCapper
In the years since nobar's comment the conditional expression documentation has been updated to say Conditional expressions (sometimes called a “ternary operator”)...Intervention
As of 2020 the comprehensive answer is in the 'Ternary Operators' documentation: "Ternary operators are more commonly known as conditional expressions [...] they became a part of Python in version 2.4."Lectern
It's also useful to use the logical or operator, such as x = a or bHostess
I have done the translation of this question on ruSO.Pursuance
F
9117

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:

Fuchsia answered 27/12, 2008 at 8:44 Comment(18)
The order may seems strange for coders however f(x) = |x| = x if x > 0 else -x sounds very natural to mathematicians. You may also understand it as do A in most case, except when C then you should do B instead...Djokjakarta
Be careful with order of operations when using this. For example, the line z = 3 + x if x < y else y. If x=2 and y=1, you might expect that to yield 4, but it would actually yield 1. z = 3 + (x if x > y else y) is the correct usage.Joshi
The point was if you want to perform additional evaluations after the conditional is evaluated, like adding a value to the result, you'll either need to add the additional expression to both sides (z = 3 + x if x < y else 3 + y), or group the conditional (z = 3 + (x if x < y else y) or z = (x if x < y else y) + 3)Joshi
what if there are multiple conditions ?Frodi
@MrGeek, you could group the boolean expressions. "foo" if (bool or bool && bool or etc) else "bar"Kid
@Kid I meant something like if (c1) a1 elif (c2) a2 elif ... else a(n).Frodi
@MrGeek, I see what you mean, so you would basically be nesting the operations: ` "foo" if Bool else ("bar" if Bool else "foobar") `Kid
Programmers need precise correct formulation even more than mathematician, because in mathematics there is always a resort to underlying concepts. A convincing argument is the % operator, mimicking the way "mod" is used in math would have been a disaster. So no, I don't accept your argument. It is like adhering to imperial units. Groetjes AlbertKlausenburg
For the expression: 'x and y' and 'x or y' and their return values I recommend checking docs.python.org/3/reference/expressions.html#boolean-operations and also the other post: #3182401Caballero
@KalZekdor I read that and expected 1... It took me a few seconds to see how you had to read it to make 4 a possibility.Sevier
Nice explanation, but short circuiting doesn't mean what you think it means - it means that the rest of the condition isn't evaluated if the 'truthiness' of the entire condition can be decided early. Statement 'b' is never evaluated even in languages without short circuiting. Ex: if (arr != null && arr[0] = 1)...Slither
As of Python 3.8+ this may not be entirely true. The walrus operator does allow assignments within expressions. Consider for instance a tiny example: a if (a := len([1,2])) > 1 else -1 as opposed to writing: len([1, 2]) if len([1, 2]) > 1 else -1.Buonaparte
@Buonaparte I edited to add a note about := and to add detail on what actually happens when trying to use = in a conditional expression. Honestly it's a pretty bad idea to write conditional expressions like that; much clearer to assign a first, since that will happen as a side effect anyway.Dordrecht
Good answer, however, that syntax looks horrible to me. They should have implemented it the C# way result = condition ? conditiontrue_expression : conditionfalse_expression instead of abusing if for that. If doesn't look like an operator at all.Cowans
You can put None to the ternary operator instead of pass and it's done. I use it very oftenEndoscope
Python tries to avoid symbolic operators where possible. if may not look enough like an operator to you, but ... ? ... : ... assumes familiarity with C to make any real sense.Mumford
@Endoscope That's fine, if None is a valid result of the expression. Usually, you should just use a regular if statement in that case.Mumford
@KalZekdor to me I wouldn't know what result your code is going to give, so I would add the appropriate brackets. I would specifically avoid learning it too and always add brackets.Hamer
P
1000

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>)]
Philomena answered 22/1, 2009 at 18:58 Comment(12)
Note that this one always evaluates everything, whereas the if/else construct only evaluates the winning expression.Roye
(lambda: print("a"), lambda: print("b"))[test==true]()Shreve
It should be noted that what's within the []s can be an arbitrary expression. Also, for safety you can explicitly test for truthiness by writing [bool(<expression>)]. The bool() function has been around since v2.2.1.Vaivode
Is this idiomatic in python? Seems confusing but maybe its conventionMatador
Ugly is in the eye of the beholder, and I don't find this ugly at all. It concisely make elegant use of the fact that bool is a subclass of int and that Python indexes are 0-based. Admittedly, it's probably not the most efficient (as @SilverBackNet mentioned, both options are eval'd). However, this works perfectly for deciding between 1 of 2 strings as @Claudiu said - I use it for this all the time. For example: '%d item%s to process!'%(num_items,('','s')[num_items > 1]) or 'Null hypothesis %s be rejected (p-val = %0.4f)'%(("can't",'must')[pval<alpha],pval).Collusion
I've done a similar trick -- only once or twice, but done it -- by indexing into a dictionary with True and False as the keys: {True:trueValue, False:falseValue}[test] I don't know whether this is any less efficient, but it does at least avoid the whole "elegant" vs. "ugly" debate. There's no ambiguity that you're dealing with a boolean rather than an int.Caiman
comparisons to singletons should always use is/is not instead of ==Judaea
This trick may help avoid timing based attacks on algorithms if it always evaluates both possible results and avoids skipping code (an 'if' skips).Josefina
@jskulski only in old python, before 2.5. Even then though, if cond: [expression_1] else: [expression_2] was more commonSevier
It's a "cool" and interesting idea, no doubt. But real-life code should be easy to read and least error-prone. If one of my developers used this I would ask him to change it. Since IMHO this should not be used in production code, I have down voted the answer.Echelon
@Caiman there's no ambiguity since bool is actually a subclass of int. And I'd argue that this answer and your variant of it are both ugly.Wendell
@MarkRansom, no argument that both are ugly (and no longer necessary) workarounds. However the answer does have a garden-path issue that my variation avoids. Put a tuple index in front of most anyone and I guarantee they will not immediately think, "oh, that variable must be representing a true/false value." They'll start by assuming it's an ordinal number and only after puzzling over the context will they realize its actual purpose. That's the ambiguity I'm talking about.Caiman
L
435

For versions prior to 2.5, there's the trick:

[expression] and [on_true] or [on_false]

It can give wrong results when on_true has a false Boolean value.1

Although it does have the benefit of evaluating expressions left to right, which is clearer in my opinion.

1. Is there an equivalent of C’s ”?:” ternary operator?

Louisiana answered 27/12, 2008 at 10:48 Comment(6)
The remedy is to use (test and [true_value] or [false_value])[0], which avoids this trap.Circumstantial
Ternary operator usually executes faster(sometimes by 10-25%).Daystar
@Daystar Do you have source for me?Fresco
@Fresco Here's the disassembled code. Using the method ThomasH suggested would be even slower.Roentgenology
Why you say "for versions prior to 2.5"? This works under 3.12.1 for me.Extrapolate
"It can give wrong results when on_true has a false Boolean value" -> an example being True and "" or "hello" which returns "hello" instead of "". This is because and returns the first operand if it is False, otherwise it returns the second. Being True True, it returns "". Now we have "" or "hello". The hello operator returns the first operand if it is True, otherwise it returns the second. Being "" False, it returns "hello".Extrapolate
M
358

<expression 1> if <condition> else <expression 2>

a = 1
b = 2

1 if a > b else -1 
# Output is -1

1 if a > b else -1 if a < b else 0
# Output is -1
Mazurek answered 27/5, 2010 at 7:56 Comment(3)
This one emphasizes the primary intent of the ternary operator: value selection. It also shows that more than one ternary can be chained together into a single expression.Tidings
@Craig , I agree, but it's also helpful to know what will happen when there are no parentheses. In real code, I too would tend to insert explicit parens.Keniakenilworth
Use: return 3 if t > 10 else t/2Lectern
P
207

From the documentation:

Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations.

The expression x if C else y first evaluates the condition, C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

See PEP 308 for more details about conditional expressions.

New since version 2.5.

Pym answered 27/12, 2008 at 8:44 Comment(0)
I
181

An operator for a conditional expression in Python was added in 2006 as part of Python Enhancement Proposal 308. Its form differs from common ?: operator and it looks like this:

<expression1> if <condition> else <expression2>

which is equivalent to:

if <condition>: <expression1> else: <expression2>

Here is an example:

result = x if a > b else y

Another syntax which can be used (compatible with versions before 2.5):

result = (lambda:y, lambda:x)[a > b]()

where operands are lazily evaluated.

Another way is by indexing a tuple (which isn't consistent with the conditional operator of most other languages):

result = (y, x)[a > b]

or explicitly constructed dictionary:

result = {True: x, False: y}[a > b]

Another (less reliable), but simpler method is to use and and or operators:

result = (a > b) and x or y

however this won't work if x would be False.

A possible workaround is to make x and y lists or tuples as in the following:

result = ((a > b) and [x] or [y])[0]

or:

result = ((a > b) and (x,) or (y,))[0]

If you're working with dictionaries, instead of using a ternary conditional, you can take advantage of get(key, default), for example:

shell = os.environ.get('SHELL', "/bin/sh")

Source: ?: in Python at Wikipedia

Inainability answered 5/5, 2015 at 12:0 Comment(2)
result = {1: x, 0: y}[a > b] is another possible variant (True and False are actually integers with values 1 and 0)Lexicography
It's wired that Expression comes first before the condition. After some practice, I can remember it now.Cly
O
123

Unfortunately, the

(falseValue, trueValue)[test]

solution doesn't have short-circuit behaviour; thus both falseValue and trueValue are evaluated regardless of the condition. This could be suboptimal or even buggy (i.e. both trueValue and falseValue could be methods and have side effects).

One solution to this would be

(lambda: falseValue, lambda: trueValue)[test]()

(execution delayed until the winner is known ;)), but it introduces inconsistency between callable and non-callable objects. In addition, it doesn't solve the case when using properties.

And so the story goes - choosing between three mentioned solutions is a trade-off between having the short-circuit feature, using at least Python 2.5 (IMHO, not a problem anymore) and not being prone to "trueValue-evaluates-to-false" errors.

Only answered 6/12, 2009 at 11:51 Comment(2)
While the tuple of lambdas trick works, it takes roughly 3x as long as the ternary operator. It's only likely to be a reasonable idea if it can replace a long chain of if else if.Kwok
this is so unreadable compared with the ternary operatorVite
F
106

Ternary operator in different programming languages

Here I just try to show some important differences in the ternary operator between a couple of programming languages.

Ternary operator in JavaScript

var a = true ? 1 : 0;
# 1
var b = false ? 1 : 0;
# 0

Ternary operator in Ruby

a = true ? 1 : 0
# 1
b = false ? 1 : 0
# 0

Ternary operator in Scala

val a = true ? 1 | 0
# 1
val b = false ? 1 | 0
# 0

Ternary operator in R programming

a <- if (TRUE) 1 else 0
# 1
b <- if (FALSE) 1 else 0
# 0

Ternary operator in Python

a = 1 if True else 0
# 1
b = 1 if False else 0
# 0
Fite answered 21/8, 2016 at 17:56 Comment(5)
This blogger found python's ternary operator to be unnecessarily different than most other languages.Minuteman
It may sound opinionated; but what it essentially says is that it the Python syntax is likely to be understood by a person who never saw a ternary operator, while very few people will understand the more usual syntax unless they have been told first what it means.Ronaldronalda
Algol68: a=.if. .true. .then. 1 .else. 0 .fi. This may be expressed also a=(.true.|1|0) As usual Algol68 is an improvement over its successors.Klausenburg
something simple as print a || '<alt text>' in ruby is pita in python print a if a is not None else 'alt text'Avoid
@VarunGarg But of course you can say print(a or 'alt text') in Python.Afterimage
E
85

For Python 2.5 and newer there is a specific syntax:

[on_true] if [cond] else [on_false]

In older Pythons, a ternary operator is not implemented but it's possible to simulate it.

cond and on_true or on_false

Though there is a potential problem, which is if cond evaluates to True and on_true evaluates to False then on_false is returned instead of on_true. If you want this behaviour the method is OK, otherwise use this:

{True: on_true, False: on_false}[cond is True] # is True, not == True

which can be wrapped by:

def q(cond, on_true, on_false)
    return {True: on_true, False: on_false}[cond is True]

and used this way:

q(cond, on_true, on_false)

It is compatible with all Python versions.

Eisenstein answered 25/4, 2012 at 11:40 Comment(2)
The behaviour is not identical - q("blob", on_true, on_false) returns on_false, whereas on_true if cond else on_false returns on_true. A workaround is to replace cond with cond is not None in these cases, although that is not a perfect solution.Sate
Why not bool(cond) instead of cond is True? The former checks the truthiness of cond, the latter checks for pointer-equality with the True object. As highlighted by @AndrewCecil, "blob" is truthy but it is not True.Janka
S
59

You might often find

cond and on_true or on_false

but this leads to a problem when on_true == 0

>>> x = 0
>>> print x == 0 and 0 or 1
1
>>> x = 1
>>> print x == 0 and 0 or 1
1

Where you would expect this result for a normal ternary operator:

>>> x = 0
>>> print 0 if x == 0 else 1
0
>>> x = 1
>>> print 0 if x == 0 else 1
1
Scriabin answered 14/1, 2013 at 15:56 Comment(0)
C
54

Does Python have a ternary conditional operator?

Yes. From the grammar file:

test: or_test ['if' or_test 'else' test] | lambdef

The part of interest is:

or_test ['if' or_test 'else' test]

So, a ternary conditional operation is of the form:

expression1 if expression2 else expression3

expression3 will be lazily evaluated (that is, evaluated only if expression2 is false in a boolean context). And because of the recursive definition, you can chain them indefinitely (though it may considered bad style.)

expression1 if expression2 else expression3 if expression4 else expression5 # and so on

A note on usage:

Note that every if must be followed with an else. People learning list comprehensions and generator expressions may find this to be a difficult lesson to learn - the following will not work, as Python expects a third expression for an else:

[expression1 if expression2 for element in iterable]
#                          ^-- need an else here

which raises a SyntaxError: invalid syntax. So the above is either an incomplete piece of logic (perhaps the user expects a no-op in the false condition) or what may be intended is to use expression2 as a filter - notes that the following is legal Python:

[expression1 for element in iterable if expression2]

expression2 works as a filter for the list comprehension, and is not a ternary conditional operator.

Alternative syntax for a more narrow case:

You may find it somewhat painful to write the following:

expression1 if expression1 else expression2

expression1 will have to be evaluated twice with the above usage. It can limit redundancy if it is simply a local variable. However, a common and performant Pythonic idiom for this use-case is to use or's shortcutting behavior:

expression1 or expression2

which is equivalent in semantics. Note that some style-guides may limit this usage on the grounds of clarity - it does pack a lot of meaning into very little syntax.

Carnap answered 17/11, 2015 at 19:14 Comment(4)
expression1 or expression2 being similar and with the same drawbacks/positives as expression1 || expression2 in javascriptDogear
Thanks, @selurvedu - it can be confusing until you get it straight. I learned the hard way, so your way might not be as hard. ;) Using if without the else, at the end of a generator expression or list comprehension will filter the iterable. In the front, it's a ternary conditional operation, and requires the else. Cheers!!Carnap
@AaronHall Although your use of metasyntactic expressionN for all instances is consistent, it might be easier to understand with naming that distinguished the conditional test expression from the two result expressions; eg, result1 if condition else result2. This is especially evident when nesting (aka chaining): result1 if condition1 else result2 if condition2 else result3. See how much better that reads this way?Magnetograph
@Magnetograph thanks for the review - if you look at the revision history, this post currently has two revisions. Most of my other answers, especially the top ones, have been revisited again and again. This answer never gets my attention because the community wiki status gives me no credit for the content, and so I never see any votes on it. As I don't really have time for an edit on this right now, frog knows when it will come to my attention again in the future. I can see you've edited the top answer, so feel free to borrow/quote my material from this post in that one (and cite me if apropos!)Carnap
L
43

One of the alternatives to Python's conditional expression

"yes" if boolean else "no"

is the following:

{True: "yes", False: "no"}[boolean]

which has the following nice extension:

{True: "yes", False: "no", None: "maybe"}[boolean_or_none]

The shortest alternative remains

("no", "yes")[boolean]

which works because issubclass(bool, int).

Careful, though: the alternative to

yes() if boolean else no()

is not

(no(), yes())[boolean]  # bad: BOTH no() and yes() are called

but

(no, yes)[boolean]()

This works fine as long as no and yes are to be called with exactly the same parameters. If they are not, like in

yes("ok") if boolean else no()  # (1)

or in

yes("ok") if boolean else no("sorry")  # (2)

then a similar alternative either does not exist (1) or is hardly viable (2). (In rare cases, depending on the context, something like

msg = ("sorry", "ok")[boolean]
(no, yes)[boolean](msg)

could make sense.)

Thanks to Radek Rojík for his comment


Addendum:

A special case of boolean indexing is when you need a single character. E.g.:

sign = '+-'[n < 0]

And finally, the plural s:

print(f"total: {n} item{'s'[n==1:]}")
Lexicography answered 9/2, 2019 at 18:23 Comment(0)
H
34

As already answered, yes, there is a ternary operator in Python:

<expression 1> if <condition> else <expression 2>

In many cases <expression 1> is also used as Boolean evaluated <condition>. Then you can use short-circuit evaluation.

a = 0
b = 1

# Instead of this:
x = a if a else b
# Evaluates as 'a if bool(a) else b'

# You could use short-circuit evaluation:
x = a or b

One big pro of short-circuit evaluation is the possibility of chaining more than two expressions:

x = a or b or c or d or e

When working with functions it is more different in detail:

# Evaluating functions:
def foo(x):
    print('foo executed')
    return x


def bar(y):
    print('bar executed')
    return y


def blubb(z):
    print('blubb executed')
    return z


# Ternary Operator expression 1 equals to False
print(foo(0) if foo(0) else bar(1))
''' foo and bar are executed once
foo executed
bar executed
1
'''

# Ternary Operator expression 1 equals to True
print(foo(2) if foo(2) else bar(3))
''' foo is executed twice!
foo executed
foo executed
2
'''

# Short-circuit evaluation second equals to True
print(foo(0) or bar(1) or blubb(2))
''' blubb is not executed
foo executed
bar executed
1
'''

# Short-circuit evaluation third equals to True
print(foo(0) or bar(0) or blubb(2))
'''
foo executed
bar executed
blubb executed
2
'''

# Short-circuit evaluation all equal to False
print(foo(0) or bar(0) or blubb(0))
''' Result is 0 (from blubb(0)) because no value equals to True
foo executed
bar executed
blubb executed
0
'''

PS: Of course, a short-circuit evaluation is not a ternary operator, but often the ternary is used in cases where the short circuit would be enough. It has a better readability and can be chained.

Haynes answered 16/10, 2019 at 8:37 Comment(0)
V
32

Simulating the Python ternary operator.

For example

a, b, x, y = 1, 2, 'a greather than b', 'b greater than a'
result = (lambda:y, lambda:x)[a > b]()

Output:

'b greater than a'
Velure answered 20/11, 2013 at 10:44 Comment(4)
Why not simply result = (y, x)[a < b] Why do you uses lambda function ?Oversize
@GrijeshChauhan Because on "compliated" expressions, e. g. involving a function call etc., this would be executed in both cases. This might not be wanted.Carrera
The use of lambda functions is an overkill for this questionSteelworker
@GrijeshChauhan In short, this implements the so-called “short-circuit evaluation”. Generally, P ? x : y or x if P else y can be written as (lambda:y, lambda:x)[P]() — but I doubt it has better performance and thus its necessity.Abramabramo
P
30
a if condition else b

Just memorize this pyramid if you have trouble remembering:

     condition
  if           else
a                   b 
Pannell answered 6/12, 2018 at 14:45 Comment(0)
M
30

Vinko Vrsalovic's answer is good enough. There is only one more thing:

Note that conditionals are an expression, not a statement. This means you can't use assignment statements or pass or other statements within a conditional expression

Walrus operator in Python 3.8

After the walrus operator was introduced in Python 3.8, something changed.

(a := 3) if True else (b := 5)

gives a = 3 and b is not defined,

(a := 3) if False else (b := 5)

gives a is not defined and b = 5, and

c = (a := 3) if False else (b := 5)

gives c = 5, a is not defined and b = 5.

Even if this may be ugly, assignments can be done inside conditional expressions after Python 3.8. Anyway, it is still better to use normal if statement instead in this case.

Matthaeus answered 23/12, 2020 at 10:1 Comment(4)
In the first example: (a := 3) if True else (b := 5) actually it's a redundant first walrus operator. This will do: a = 3 if True else (b := 5)Unstrung
@AndrewAnderson No it's not redundant. You should compare both the first and the second examples. You can combine them and consider this: (a := 3) if x else (b := 5), you always get either a or b assigned, not both. However, consider a = 3 if x else (b := 5), when x == False, you will get a = 5 and b = 5, where both them are assigned.Canterbury
Yes, that's correct :). I considered this only for x=True case which is of course is limited.Unstrung
Because we don't really write down this code if True else, the reason of the first example is only compared with other examples.Canterbury
P
28

The ternary conditional operator simply allows testing a condition in a single line replacing the multiline if-else making the code compact.

Syntax:

[on_true] if [expression] else [on_false]

1- Simple Method to use ternary operator:

# Program to demonstrate conditional operator
a, b = 10, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(min)  # Output: 10

2- Direct Method of using tuples, Dictionary, and lambda:

# Python program to demonstrate ternary operator
a, b = 10, 20
# Use tuple for selecting an item
print( (b, a) [a < b] )
# Use Dictionary for selecting an item
print({True: a, False: b} [a < b])
# lambda is more efficient than above two methods
# because in lambda  we are assure that
# only one expression will be evaluated unlike in
# tuple and Dictionary
print((lambda: b, lambda: a)[a < b]()) # in output you should see three 10

3- Ternary operator can be written as nested if-else:

# Python program to demonstrate nested ternary operator
a, b = 10, 20
print ("Both a and b are equal" if a == b else "a is greater than b"
        if a > b else "b is greater than a")

Above approach can be written as:

# Python program to demonstrate nested ternary operator
a, b = 10, 20
if a != b:
    if a > b:
        print("a is greater than b")
    else:
        print("b is greater than a")
else:
    print("Both a and b are equal")
# Output: b is greater than a
Perla answered 4/4, 2018 at 14:2 Comment(1)
Note that the ternary operator is smaller (in memory) and faster than the nested if. Also, your nested if-else isn't actually a rewrite of the ternary operator, and will produce different output for select values of a and b (specifically if one is a type which implements a weird __ne__ method).Kwok
L
27

You can do this:

[condition] and [expression_1] or [expression_2];

Example:

print(number%2 and "odd" or "even")

This would print "odd" if the number is odd or "even" if the number is even.


The result: If condition is true, exp_1 is executed, else exp_2 is executed.

Note: 0, None, False, emptylist, and emptyString evaluates as False.

And any data other than 0 evaluates to True.

Here's how it works:

If the condition [condition] becomes "True", then expression_1 will be evaluated, but not expression_2.

If we "and" something with 0 (zero), the result will always to be false. So in the below statement,

0 and exp

The expression exp won't be evaluated at all since "and" with 0 will always evaluate to zero and there is no need to evaluate the expression. This is how the compiler itself works, in all languages.

In

1 or exp

the expression exp won't be evaluated at all since "or" with 1 will always be 1. So it won't bother to evaluate the expression exp since the result will be 1 anyway (compiler optimization methods).

But in case of

True and exp1 or exp2

The second expression exp2 won't be evaluated since True and exp1 would be True when exp1 isn't false.

Similarly in

False and exp1 or exp2

The expression exp1 won't be evaluated since False is equivalent to writing 0 and doing "and" with 0 would be 0 itself, but after exp1 since "or" is used, it will evaluate the expression exp2 after "or".


Note:- This kind of branching using "or" and "and" can only be used when the expression_1 doesn't have a Truth value of False (or 0 or None or emptylist [ ] or emptystring ' '.) since if expression_1 becomes False, then the expression_2 will be evaluated because of the presence "or" between exp_1 and exp_2.

In case you still want to make it work for all the cases regardless of what exp_1 and exp_2 truth values are, do this:

[condition] and ([expression_1] or 1) or [expression_2];
Labdanum answered 20/8, 2017 at 7:31 Comment(1)
If you want to use that in the context of x = [condition] and ([expression_1] or 1) or [expression_2] and expression_1 evaluates to false, x will be 1, not expression_1. Use the accepted answer.Becoming
I
26

More a tip than an answer (I don't need to repeat the obvious for the hundredth time), but I sometimes use it as a one-liner shortcut in such constructs:

if conditionX:
    print('yes')
else:
    print('nah')

, becomes:

print('yes') if conditionX else print('nah')

Some (many :) may frown upon it as unpythonic (even, Ruby-ish :), but I personally find it more natural - i.e., how you'd express it normally, plus a bit more visually appealing in large blocks of code.

Idyllist answered 11/5, 2016 at 7:13 Comment(4)
I prefer print( 'yes' if conditionX else 'nah' ) over your answer. :-)False
That is if you want to print() in both cases - and it looks a bit more pythonic, I have to admit :) But what if the expressions/functions are not the same - like print('yes') if conditionX else True - to get the print() only in truthy conditionX Idyllist
To add to Frederick99's remark, another reason to avoid print('yes') if conditionX else print('nah') is that it gives a SyntaxError in Python2.Angelaangele
The only reason it gives a syntax error is because in Python 2 print is a statement - print "yes", while in Python 3 it is a function - print("yes"). That can be resolved by either using it as a statement, or better - from future import print_function.Idyllist
P
17

Many programming languages derived from C usually have the following syntax of the ternary conditional operator:

<condition> ? <expression1> : <expression2>

At first, the Python's benevolent dictator for life (I mean Guido van Rossum, of course) rejected it (as non-Pythonic style), since it's quite hard to understand for people not used to C language. Also, the colon sign : already has many uses in Python. After PEP 308 was approved, Python finally received its own shortcut conditional expression (what we use now):

<expression1> if <condition> else <expression2>

So, firstly it evaluates the condition. If it returns True, expression1 will be evaluated to give the result, otherwise expression2 will be evaluated. Due to lazy evaluation mechanics – only one expression will be executed.

Here are some examples (conditions will be evaluated from left to right):

pressure = 10
print('High' if pressure < 20 else 'Critical')

# Result is 'High'

Ternary operators can be chained in series:

pressure = 5
print('Normal' if pressure < 10 else 'High' if pressure < 20 else 'Critical')

# Result is 'Normal'

The following one is the same as previous one:

pressure = 5

if pressure < 20:
    if pressure < 10:
        print('Normal')
    else:
        print('High')
else:
    print('Critical')

# Result is 'Normal'
Popcorn answered 25/12, 2018 at 13:4 Comment(0)
M
15

Yes, Python have a ternary operator, here is the syntax and an example code to demonstrate the same :)

#[On true] if [expression] else[On false]
# if the expression evaluates to true then it will pass On true otherwise On false

a = input("Enter the First Number ")
b = input("Enter the Second Number ")

print("A is Bigger") if a>b else print("B is Bigger")
Montsaintmichel answered 21/10, 2018 at 20:14 Comment(4)
I have added a one line statement example to check which number is big to elaborate it furtherMontsaintmichel
print is really not a good choice, as this will give a SyntaxError in Python2.Angelaangele
@Thierry Lathuille here I used print() function not print statement, print function is for Python 3 while print statement is for Python 2Montsaintmichel
The question has already been asked on SO, just try it with Python 2 and you will see by yourself. 'print('hello') is a perfectly valid syntax in Python 2.7, but the way it is parsed makes your code above throw a SyntaxError.Angelaangele
B
12

Other answers correctly talk about the Python ternary operator. I would like to complement by mentioning a scenario for which the ternary operator is often used, but for which there is a better idiom. This is the scenario of using a default value.

Suppose we want to use option_value with a default value if it is not set:

run_algorithm(option_value if option_value is not None else 10)

or, if option_value is never set to a falsy value (0, "", etc.), simply

run_algorithm(option_value if option_value else 10)

However, in this case an ever better solution is simply to write

run_algorithm(option_value or 10)
Beryl answered 19/5, 2020 at 16:47 Comment(4)
A valuable complement, but I disagree: option_value or 10 is not better than option_value if option_value is not None else 10. It is shorter, indeed, but looks weird to me and may lead to bugs. What happens if option_value = 0, for instance? The first snippet will run run_algorithm(0) because option_value is not None. The second and third snippets, however, will run run_algorithm(10) because 0 is a falsy. The two snippets are not equivalent, and hence one is not better than the other. And explicit is better than implicit.Teter
@ruancomelli: Good point. I've modified the answer to reflect that correction.Beryl
As for it looking weird, I wonder if it looked weird to you because you noticed the imprecision (that it was not really equivalent). To me it sounds natural because it reminds me saying in English: "Use this or that (if the first option is unavailable)". But of course that is subjective. It is useful to know it does not look natural to everybody.Beryl
Much better! And thanks for the explanation regarding the "or"-idiom. It looks weird to me because I tend to think of or as a function mapping two arguments to a boolean, so I expect it to return either True or False (this happens in many other programming languages). But "use this or that" is a nice mnemonic and will definitely help me (and hopefully others) to remember this pattern.Teter
A
12

The syntax for the ternary operator in Python is:

[on_true] if [expression] else [on_false]

Using that syntax, here is how we would rewrite the code above using Python’s ternary operator:

game_type = 'home'
shirt = 'white' if game_type == 'home' else 'green'

It's still pretty clear, but much shorter. Note that the expression could be any type of expression, including a function call, that returns a value that evaluates to True or False.

Aixenprovence answered 10/4, 2022 at 17:39 Comment(0)
O
10

Python has a ternary form for assignments; however there may be even a shorter form that people should be aware of.

It's very common to need to assign to a variable one value or another depending on a condition.

>>> li1 = None
>>> li2 = [1, 2, 3]
>>>
>>> if li1:
...     a = li1
... else:
...     a = li2
...
>>> a
[1, 2, 3]

^ This is the long form for doing such assignments.

Below is the ternary form. But this isn't the most succinct way - see the last example.

>>> a = li1 if li1 else li2
>>>
>>> a
[1, 2, 3]
>>>

With Python, you can simply use or for alternative assignments.

>>> a = li1 or li2
>>>
>>> a
[1, 2, 3]
>>>

The above works since li1 is None and the interpreter treats that as False in logic expressions. The interpreter then moves on and evaluates the second expression, which is not None and it's not an empty list - so it gets assigned to a.

This also works with empty lists. For instance, if you want to assign a whichever list has items.

>>> li1 = []
>>> li2 = [1, 2, 3]
>>>
>>> a = li1 or li2
>>>
>>> a
[1, 2, 3]
>>>

Knowing this, you can simply such assignments whenever you encounter them. This also works with strings and other iterables. You could assign a whichever string isn't empty.

>>> s1 = ''
>>> s2 = 'hello world'
>>>
>>> a = s1 or s2
>>>
>>> a
'hello world'
>>>

I always liked the C ternary syntax, but Python takes it a step further!

I understand that some may say this isn't a good stylistic choice, because it relies on mechanics that aren't immediately apparent to all developers. I personally disagree with that viewpoint. Python is a syntax-rich language with lots of idiomatic tricks that aren't immediately apparent to the dabbler. But the more you learn and understand the mechanics of the underlying system, the more you appreciate it.

Omniscient answered 11/3, 2020 at 6:49 Comment(4)
Something seems to be missing near "simply such assignments".Genous
"Simplify" such assignments =) @PeterMortensenOmniscient
Very interesting! Two questions: 1) Clearly, if exactly one of li1 and li2 is truthy, then li1 or li2 will return its value. If bool(li1) returns the same value as bool(li2), will li1 or li2 consistently evaluate to be either one or the other (perhabs by position)? 2) how can you interpret what returns from li1 and li2? I can't make sense of it with my test cases.Trial
@TimH: 1) The right hand or subexpressions of the assignment are interpreted left to right. If the first one is truthy, that's the one that gets assigned to the var. If it isn't truthy, the second one is assigned. 2) using the and operation wouldn't be like some sort of ternary operation as this answer discusses.Omniscient
S
7

Pythonic way of doing the things:

"true" if var else "false"

But there always exists a different way of doing a ternary condition too:

"true" and var or "false"
Synaesthesia answered 29/12, 2021 at 18:53 Comment(0)
L
4

There are multiple ways. The simplest one is to use the condition inside the "print" method.

You can use

print("Twenty" if number == 20 else "Not twenty")

Which is equivalent to:

if number == 20:
    print("Twenty")
else:
    print("Not twenty")

In this way, more than two statements are also possible to print. For example:

if number == 20:
    print("Twenty")
elif number < 20:
    print("Lesser")
elif 30 > number > 20:
    print("Between")
else:
    print("Greater")

can be written as:

print("Twenty" if number == 20 else "Lesser" if number < 20 else "Between" if 30 > number > 20 else "Greater")
Livvyy answered 6/2, 2022 at 13:46 Comment(0)
B
3

The if else-if version can be written as:

sample_set="train" if "Train" in full_path else ("test" if "Test" in full_path else "validation")
Ballew answered 1/2, 2022 at 13:23 Comment(0)
C
3

Yes, it has, but it's different from C-syntax-like programming languages (which is condition ? value_if_true : value_if_false

In Python, it goes like this: value_if_true if condition else value_if_false

Example: even_or_odd = "even" if x % 2 == 0 else "odd"

Canales answered 16/2, 2022 at 23:9 Comment(0)
C
1

A neat way to chain multiple operators:

f = lambda x,y: 'greater' if x > y else 'less' if y > x else 'equal'

array = [(0,0),(0,1),(1,0),(1,1)]

for a in array:
  x, y = a[0], a[1]
  print(f(x,y))

# Output is:
#   equal,
#   less,
#   greater,
#   equal

Chrominance answered 12/5, 2019 at 13:3 Comment(0)
W
1

Yes, Python has a ternary conditional operator, also known as the conditional expression or the ternary operator. The syntax of the ternary operator in Python is:

value_if_true if condition else value_if_false

Here's an example to illustrate its usage:

x = 5
result = "Even" if (x % 2 == 0) else "Odd"
print(result)

In this example, the condition x % 2 == 0 checks if x is divisible by 2. If the condition is True, the value "Even" is assigned to the variable result. Otherwise, the value "Odd" is assigned. The output will be:

Odd

It is a good idea to use parenthesis, in order to increase readability, when working with medium complexity operations:

"Even" if (x % 2 == 0) else "Odd"

Instead of:

"Even" if x % 2 == 0 else "Odd"

The ternary operator is a concise way to write simple conditional expressions in a single line. It can be particularly useful when assigning values or constructing expressions based on conditions. However, for more complex conditions or longer expressions, if possible it's generally better to use if-else statements for improved readability.

Worlock answered 5/6, 2023 at 18:26 Comment(0)
C
0

I find the default Python syntax val = a if cond else b cumbersome, so sometimes I do this:

iif = lambda (cond, a, b): a if cond else b
# So I can then use it like:
val = iif(cond, a, b)

Of course, it has the downside of always evaluating both sides (a and b), but the syntax is way clearer to me.

Critta answered 26/3, 2020 at 21:59 Comment(3)
This seems to be twice the amount of work, more RAM usage and more obfuscated than the simpler val = a if cond else b statement.Photoperiod
Also both a and b get evaluated here every time, unlike in a if cond else bSatiable
According to PEP8 assigning lambda to variable is a code smell. Lambda should be used only as inplace function.Entropy
H
0

I have data coming from a device as a string and leaving it as a rebuild string. Conditional expression need to be limited by (). This allow to have multiple conditions to build the string in one line. If not it seems like whatever after the "else" will be accounted.

    d0 = "-679 58 1029"
    d1 = d0.split(" ")

    strg = (d1[0][:-2] if len(d1[0])>= 3 else "0") + " " +d1[0][-2:]+ " "+ (d1[1][:-2] if len(d1[1])>= 3 else "0") + " " + d1[1][-2:] + " " +d1[2]
    print(strg)
Hereunto answered 13/2, 2023 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.