0

I'm a newbie at Python. This is my code right here. When I do not add parenthesis around x<=y<=z it gives me a false bool value in return and the if statement doesn't run.

def is_between(x,y,z):
    return x<=y<=z==1

if is_between(1,2,4):
    print('y is between x and z')

But if I add the parenthesis, it works perfectly fine. But why is this happening? I checked operator precedence and <= has higher precedence than the == operator. Shouldn't x<=y<=z evaluate first even if I don't add those parentheses?

def is_between(x,y,z):
    return (x<=y<=z)==1

if is_between(1,2,4):
    print('y is between x and z')
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Minhaj Ansari
  • 43
  • 1
  • 3
  • Also, I don't know who told you that `<=` has higher precedence than `==`, but they're wrong. [`<=` and `==` have the same precedence, which they share with all the comparison operators.](https://docs.python.org/3/reference/expressions.html#operator-precedence) – Patrick Haugh Jul 11 '18 at 14:23
  • I read it at https://www.tutorialspoint.com/python/operators_precedence_example.htm I guess I should have consulted the docs for authentic information. – Minhaj Ansari Jul 11 '18 at 15:22

0 Answers0