2

I'm trying to see if a function returns an integer who's value should be either a 1 or 0.

0 == (1 or 0)

0 is equal to 1 or 0, this sounds like it should be true, but it's not.

Why? And how to do what I'm looking to do correctly?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
JC123
  • 161
  • 7

3 Answers3

5

0 == (1 or 0) is parsed as this tree:

  ==
 /  \
0    or
    /  \
   1    0

1 or 0 results in 1, because or returns the first truthy value of the two operands (and 1 is truthy).

Afterwards, we're left with 0 == 1, which is obviously False.

What you want to do, is check whether 0 is one of those values which you can do through a sequence check: 0 in (0, 1)

L3viathan
  • 26,748
  • 2
  • 58
  • 81
4

1 or 0 evaluates to 1, and since 0 is not equal to 1, the expression is false.

I suspect what you are trying to do is something like 0 == 0 or 1 == 0

L3viathan
  • 26,748
  • 2
  • 58
  • 81
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
0

You are using the brackets. Which means you are forcing precedence on it. so python will first evaluate the expression 1 or 0 which is 1. and then it will evaluate the next part, 0 == 1 which is false.