-1

I need to create a list consisting of 10 different Python objects that are falsy. The list must have the property that a is b evaluates to False whenever a and b are entries of the list in different positions. I find one, but two entries confuses me: 0<0 and False. I write some codes to test whether the two objects are identical.

 bool(0>0 is False)

The result is False.

But when I run the code:

mylist=[0>0,False]
bool(mylist[0] is mylist[1])

it returns True.

So are the two objects identical or not in Python?

  • 1
    ```print(bool((0>0)== False))```, I think that it is evaluating the first expression. This will give what you want –  Jun 29 '21 at 13:18
  • Your first expression is not what you think it is due to operator precedence in python. – quamrana Jun 29 '21 at 13:22
  • If you print out mylist you will see you answer. mylist returns [False, False] because it evaluates the expressions first then perform the "is" operator. – VRComp Jun 29 '21 at 13:22
  • @VRComp: I don't think the OP is questioning that the individual items are falsy. – quamrana Jun 29 '21 at 13:23
  • @quamrana you are correct. I was just providing a reason why this might be happening due to operator precedence as you pointed out above. – VRComp Jun 29 '21 at 13:25

3 Answers3

3

Looking at the docs, look at the note:

Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section.

Bold is mine.

Basically, your expression 0>0 is False is equivalent to (0>0) and (0 is False), which is False and False, so bool(False) -> False.

As mentioned in other answers, adding brackets will solve the problem (0 > 0) is False

buran
  • 13,682
  • 10
  • 36
  • 61
2

Your first line is considered as

0 > (0 is False)

Put it this way and it will work:

(0 > 0) is False   # ==> True
zwitsch
  • 359
  • 3
  • 9
1

You need to add parentheses...

bool(0>0 is False)

is False because the steps are to first calculate 0 is False and then checking whether 0 > (0 is False). python "chains" logical clauses.
Consider for instance a more familiar case of chaining:

4 > 2 > 9

which is equivalent to

(4 > 2) and (2 > 9)  # => True and False => False

So the chaining happening in your case is equivalent to

(0 > 0) and (0 is False)  # => False and False (+ syntax-warning) => False

which is, of course, False.

If you try the following though -

bool((0 > 0) is False)

You get the required True


Edited to actually be correct thanks to comments and based on @buran's answer. Thanks for the correction and sorry for the original answer being misleading.

ShlomiF
  • 2,686
  • 1
  • 14
  • 19
  • The explanation is wrong. `>` and `is` have the same precedence, as stated in https://docs.python.org/3/reference/expressions.html#operator-precedence, so chaining takes place. See @buran's answer for a correct explanation. – Thierry Lathuille Jul 07 '21 at 19:40
  • You're right. Thanks! Corrected and referred. – ShlomiF Jul 07 '21 at 21:10