6

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?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
shooqie
  • 950
  • 7
  • 17
  • Related useful information, the expression ` and X` will resolve to `X` (or `False`, if `` is `False`). Similar to how the `or` operator works. So it's a combination of two operators, not a special syntax. – SuperBiasedMan Aug 22 '16 at 13:12
  • The explanation is in here, once you break down the steps of the process: [Python's Logical Operator AND](http://stackoverflow.com/questions/18195322/pythons-logical-operator-and) – SuperBiasedMan Aug 22 '16 at 13:15
  • 1
    You may find [this answer](http://stackoverflow.com/a/36551857/4014959) helpful. – PM 2Ring Aug 22 '16 at 13:46

5 Answers5

8

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)

awesoon
  • 32,469
  • 11
  • 74
  • 99
4

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
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

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.

Kalpesh Dusane
  • 1,477
  • 3
  • 20
  • 27
0

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.

Community
  • 1
  • 1
vsminkov
  • 10,912
  • 2
  • 38
  • 50
0

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.

qwerty
  • 116
  • 5