Python Forum
why (a and b) different from (b and a) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: why (a and b) different from (b and a) (/thread-25677.html)



why (a and b) different from (b and a) - oloap - Apr-07-2020

suppose that a = (True, 2) and b = (False, False). Why do a and b is different from b and a? Specifically, a and b == (False, False), and b and a == (True, 2)


RE: why (a and b) different from (b and a) - buran - Apr-07-2020

Look at https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not

If you have result = expression1 and expresion2 and expression3 when interpreter will evaluate the expressions from left to right and:
if all are True, the value of result will be the last expression that is True (expression3)
if not all are True (e.g. expression2 is False) the value of result will return the first False value (i.e. expression2).

If you have result = expression1 or expresion2 or expression3, then:
if any is True, result value will be the first True value
if all are False, result value will be last False value

In your case both a and b are True (non-empty tuples), so it always returns the second one

it will be good to look at https://docs.python.org/3/library/stdtypes.html#truth-value-testing


RE: why (a and b) different from (b and a) - bowlofred - Apr-07-2020

From the Python Documentation

Quote:x and y --- if x is false, then x, else y

In other words, whenever the first item in an and is true, return the second item.

So for your tuples a and b, are they false or true?

Quote:By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. 1 Here are most of the built-in objects considered false:
  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

Since neither of your tuples are empty, they are both evaluated as true. That means you'll always see the second item returned.

>>> 1 and 2
2
>>> 2 and 1
1



RE: why (a and b) different from (b and a) - oloap - Apr-10-2020

Ok thanks, but I have still a doubt. Why do (True, 2) and (False, False) = (False, False) and (False, False) and (True, 2) = (True, 2) ? How can I apply your approach to tuples?


RE: why (a and b) different from (b and a) - buran - Apr-10-2020

Look at (True, 2) and (False, False). Here you have exprA and exprB, where exprA=(True, 2) and exprB=(False, False).
It will evaluate True value of each expr from left to right. These are non-empty tuples, so they both are considered to be True. It's a and operator and in the link I shared is explained how it works.

Quote:x and y ==> if x is false, then x, else y

>>> bool((True, 2))
True
>>> bool((False, False))
True
>>> (True, 2) and (False, False)
(False, False)
>>> (False, False) and (True, 2)
(True, 2)