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)
#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


Messages In This Thread
why (a and b) different from (b and a) - by oloap - Apr-07-2020, 07:24 PM
RE: why (a and b) different from (b and a) - by bowlofred - Apr-07-2020, 08:06 PM

Forum Jump:

User Panel Messages

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