shooqie
shooqie

Reputation: 1022

X and Y or Z - ternary operator

In Java or C we have <condition> ? X : Y, which translates into Python as X if <condition> else Y.

But there's also this little trick: <condition> and X or Y.

While I understand that it's equivalent to the aforementioned ternary operators, I find it difficult to grasp how and and or operators are able to produce correct result. What's the logic behind this?

Upvotes: 6

Views: 5293

Answers (5)

Kalpesh Dusane
Kalpesh Dusane

Reputation: 1495

I think that first it will check <condition> if it's True then it execute X and skip executing Y if X evaluate to True

But if <condition> fails then it skip executing X and execute OR statement and execute Y.

Upvotes: 1

DeepSpace
DeepSpace

Reputation: 81654

If we examine A and B, B will be evaluated only if A is True.

Like so, in A or B, B will only be evaluated in case A is False.

Therefore, <condition> and X or Y will return X if <condition> is True and Y if <condition>is False. This is a result of short-circuiting and the fact that and has precedence over or.

However, you should be careful with this approach. If X itself is evaluated to False (eg an empty string, list or 0), <condition> and X or Y will return Y even if <condition> is True:

X = 1
Y = 2

print(True and X or Y)
>> 1

compared to:

X = 0  # or '' or []
Y = 2

print(True and X or Y)
>> 2

Upvotes: 4

qwerty
qwerty

Reputation: 116

This makes use of the fact that precedence of and is higher than or.

So <condition> and X or Y is basically (<condition> and X) or Y. If <condition> and X evaluates to True, there is no need to evaluate further, as anything True or Y is True. If <condition> and X evaluates to False, then Y is returned as False or Y is basically Y.

Upvotes: 0

vsminkov
vsminkov

Reputation: 11260

The trick is how python boolean operators work

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Upvotes: 0

awesoon
awesoon

Reputation: 33691

While I understand that it's equivalent to the aforementioned ternary operators

This is incorrect:

In [32]: True and 0 or 1
Out[32]: 1

In [33]: True and 2 or 1
Out[33]: 2

Why the first expression returns 1 (i.e. Y), while the condition is True and the "expected" answer is 0 (i.e. X)?

According to the docs:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

So, True and 0 or 1 evaluates the first argument of the and operator, which is True. Then it returns the second argument, which is 0.

Since the True and 0 returns false value, the or operator returns the second argument (i.e. 1)

Upvotes: 8

Related Questions