Python Forum
why (a and b) different from (b and a)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why (a and b) different from (b and a)
#1
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)
Reply
#2
Look at https://docs.python.org/3/library/stdtyp...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/stdtyp...ue-testing
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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?
Reply
#5
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020