0

This is a small element of a larger script. Through trial and error I have found that this is the offending line:

>>> print('lower than all') if (3 < (2 and 9 and 9)) is True else print('false')
lower than all

This is clearly incorrect. However changing to this gives the right answer:

>>> print('lower than all') if 3 < (2 and 9 and 9) is True else print('false')
false

I have simplified the example, in reality all the numbers are variables being checked. I really don't understand the difference here!

Again why does this work correctly?!

>>> print('lower than all') if 3 < 2 and 9 and 9 is True else print('false')
false
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Easiest way to understand these is to break them down chunk by chunk. – blackbrandt Aug 09 '22 at 17:21
  • 3
    None of these work correctly, they're all fundamentally wrong and failing in different ways. – CJR Aug 09 '22 at 17:27
  • There's too much going on here to give just one answer. Like, why are you checking `is True`? That's redundant for testing a boolean expression. And why is there a redundant `9`? Most importantly, what are you expecting `2 and 9 and 9` to mean? I think this is ultimately a duplicate of these questions: [How to test multiple variables for equality against a single value?](/q/15112125/4518341), [Why does "a == x or y or z" always evaluate to True?](/q/20002503/4518341) – wjandrea Aug 09 '22 at 17:29
  • 1
    Sorry, forgot to lead with this: Welcome to Stack Overflow! Please take the [tour]. Check out [ask], which has tips like how to refine your question. – wjandrea Aug 09 '22 at 17:30
  • 1
    Another duplicate: [Comparison operators and 'is' - operator precedence in python?](/q/32182177/4518341) – wjandrea Aug 09 '22 at 17:32
  • After reading it over again, it looks like every aspect of what you're asking is covered by those questions, so I've closed it accordingly. But my points about the redundant `is True` and `and 9` are still relevant. – wjandrea Aug 09 '22 at 17:36
  • Oops, one aspect the other questions don't cover is how to do multiple comparisons besides `==`. One sec, lemme find another one... – wjandrea Aug 09 '22 at 17:38
  • BTW, the second `print()` is redundant. You can do something like `print(x if condition else y)` – wjandrea Aug 09 '22 at 17:44

1 Answers1

0

If you see this chunk, it returns 9

>>> 2 and 9 and 9
9

To understand this, take a look at how the and operation between integers works. From the official doc:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

In this case, 2 is true, 9 is true and the final 9 is also true. Here, the last 9 is y and that's the value being returned.

Try out 2 and 1 and you'll see 1 as the result.

Due to this, your statement

print('lower than all') if (3 < (2 and 9 and 9)) is True else print('false')

Actually becomes

print('lower than all') if (3 < 9) is True else print('false')

And prints 'lower than all'

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sruthi
  • 2,908
  • 1
  • 11
  • 25
  • I just edited this to fix the details. You wrote that "`2` returns `True`", which is not really how that works. Look into ["truthy" and "falsy"](/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false) to understand it better. – wjandrea Aug 09 '22 at 18:02