1

Look at this code at Python:

a='a'
print(a==('a' or 'b'))
a='b'
print(a==('a' or 'b'))

The output will be:

True
False

Can you explain me why please?

Thank you!

PCIL
  • 41
  • 1
  • 4
  • 3
    Did you check what `'a' or 'b'` evaluated to? – silel Apr 28 '17 at 15:26
  • 1
    This will probably soon be closed as a duplicate, even though it's hard to find via search. The answer is in the way the `or` operator works, look it up in the documentation. – Mark Ransom Apr 28 '17 at 15:27
  • Try `print('a' or 'b')` – Barmar Apr 28 '17 at 15:27
  • Now I see, thank you!! – PCIL Apr 28 '17 at 15:31
  • Have you tried using | instead of or - (| = pipe) https://www.tutorialspoint.com/python/python_basic_operators.htm – SCramphorn Apr 28 '17 at 15:40
  • @SCramphorn did you? `'a' | 'b'` is an error. In general, `|` (bitwise or) has a rather different purpose to `or` (logical or), and can give very different result, meaning that suggesting it as a drop-in replacement is rarely helpful outside of situations like numpy arrays filled with bools. – lvc Apr 28 '17 at 16:14

4 Answers4

3

To break it down, The parenthesis get evaluated first. So, "a" or "b" - "a" is truthy and returns itself. "b" never gets evaluated because a non empty string will always be truthy.

To get a better idea of this, run it by itself in a prompt

>>> ('a' or 'b') 
'a'

Thus you end up with 'a' == 'a' - which is true

IN the second example, a is set to 'b' so the same thing happens, only 'b' ≠ 'a' so it returns false

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
2

@rm-vanda is correct.

I believe the behavior you expect is better found using lists or tuples:

>>> a = "b"
>>> a in ["a", "b"]
True
lysdexia
  • 1,786
  • 18
  • 29
0

When you have an expression like 'a' or 'b', it will return the first value that is not False. That being said, in both cases the expression will return 'a'.

I assume you can figure out the rest of it on your own, but the first is True because you equate 'a' == 'a', and in the second 'b' == 'a'.

m_callens
  • 6,100
  • 8
  • 32
  • 54
  • 2
    _that is not `None`_, didn't you mean _that is not `False`_? – silel Apr 28 '17 at 15:29
  • 1
    "not `None`" is a little bit inaccurate. There are lots of other values besides `None` that will fail too. – Mark Ransom Apr 28 '17 at 15:29
  • Correction made – m_callens Apr 28 '17 at 15:31
  • 1
    "not `False`" isn't accurate either. The truth is more complicated and subtle. – Mark Ransom Apr 28 '17 at 15:35
  • `''` is "not `False`", but check what `'' or 'b'` gives you. The rule is actually whether the LHS is *truthy* - ie, whether `bool(LHS)` is True (and strings, like other builtin sequences and containers, are truthy exactly when they are nonempty). Also, note that `x or y` only ever checks the truthiness of `x` - if it is truthy, the result is `x`, otherwise it is `y`. – lvc Apr 28 '17 at 15:43
0

('a' or 'b') will always resolve as 'a' because 'a' is resolved as True in a boolean context.

x or y returns the value of x if x is True (= different of None, False, "", (,), [] or {}), else it returns the value of y.

Tryph
  • 5,946
  • 28
  • 49