1

I'm confused by comparison operators. For example,

 10 or 20 == 20
 # output, expected True
 10

  10 | 20 == 20
 (10 | 20) == 20
 (10 or 20) == 20

All 3 lines of code give 'False', yet I was expecting 'True'.

 10 or 20 == 20
 # output gives 10, but was expecting True
 10

Another example:

 10 and 20 > 2
 # output is as expected
 True

 (10 and 20) > 2
 True

 (10 & 20) > 2
 # output gives False, but was expecting True
 False

Lastly, if I do:

 10 or 20 > 100
 #output is 10. No idea why
 10
 3 or 8 < 200
 3

Can anyone help clear up this confusion? Much appreciated for taking the time to read my perplexion! I'm using Python 3.6

MichaelRSF
  • 886
  • 5
  • 16
  • 40
  • Just print every expression individually and then you will understand the logic of their composits. E.g `(10 or 20)` is 10, but `(10 | 20)` is 30. – Zefick Sep 27 '17 at 11:47
  • 1
    `10 & 20` is bitwise `and`, so `01010 & 10100` (in binary) which is indeed `00000` which is not higher than 2 – Rafalon Sep 27 '17 at 11:59

2 Answers2

2

Both of these conditional operators would return the last condition or value they had to evaluate.

or operator evaluates if either of those conditions are true, and return the last one it evaluated. Since 10 is considered True in Python (or any other language for that matter), the language won't even go through the second condition (or value) and just return the first value. Whereas in case of and both conditions have to be true, and the second value would be returned if both are truthy, and the first one if not.

>>> True or False
True
>>> False or True
True
>>> True and False
False

# Similarly
>>> 10 or 20
10
>>> 10 and 20
20
>>> 0 or 10
10
>>> 0 and 10
0

This behaviour also provides a convenient alternative for certain b = a if a else c kind of behaviour.

hspandher
  • 15,934
  • 2
  • 32
  • 45
  • Thanks hspandher, but to be honest, I find the logic counter-intuitive. I don't think it's unreasonable to have say `10 or 20 == 20` and expect `True` rather than 10. And if only the first number is being evaluated, then what's the point in comparison chain operators, i.e. `(10 or 20 or 30) > 15` which gives `False`, not `10`. – MichaelRSF Sep 27 '17 at 11:59
  • @MichaelRSF There might be certain cons to this behaviour, but more often than not it proves quite convenient. I don't know how many times I have used this thing as a convenient alternative for `if-else` construct. – hspandher Sep 27 '17 at 12:04
0

It is similar to True or false case :

>>> 20 or 10
20
>>>(20 or 10) == 10 
False
>>>(20 or 10) == 20
True

This is because It takes the first value as true and next as false and when you pass true or false you get true similarly you get 20 which actually represents true here. Hope this helps.

Taniya
  • 248
  • 2
  • 8
  • Thanks user8477766. My problem is that it seems almost pointless doing a chain comparison operator if everytime only the first number is evaluated/being compared using the `==` operator. Might as well just do `(20) == 10` gives `True` Not to sound stuck up, but I personally dislike the "logic" behind it. Seems no better than `True or False == True` gives `True`. Pointless running the code when the output is known before hand. – MichaelRSF Sep 27 '17 at 12:22
  • @MichaelRSF your example there seems pointless because *it is pointless*. When you chain comparisons the inputs *aren’t* re-evaluated, which is why `a < b() < c` is better than `a < b() and b() < c`. But it’s unclear why you thought you could use `or` or `|` like that, maybe see also https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values – jonrsharpe Sep 27 '17 at 12:29