-1

could someone please explain to me why this code returns True? I totally don't understand this one, for my understanding, it should return False.

z = 2
y = 1
x = y < z or z > y and y > z or z < y
print(x)
petezurich
  • 9,280
  • 9
  • 43
  • 57
darkK
  • 101
  • 5

2 Answers2

0

and binds more (based on operator precendence) than or does. Hence, your boolean expression could also be represented like this:

z = 2
y = 1
x = y < z or (z > y and y > z) or z < y
print(x)

Clearly, y < z is True and because only one of the three needs to be True, the whole expression evaluates to True.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
0

or has a lower operator precedence than and, so y < z or z > y and y > z or z < y is equivalent to (y < z) or (z > y and y > z) or (z < y), and since y < z is True, the entire expression is therefore True after the evaluation of the or operations.

blhsing
  • 91,368
  • 6
  • 71
  • 106