-1
z=2
y=1

I have the expression:

x = y<z or z>y and y>z or z<y

Can somebody explain how this evaluates to True?

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
Sudheer
  • 19
  • 2
  • [mre] - add print statements – Patrick Artner May 27 '22 at 10:18
  • Most expressions are compound of smaller expressions. What you wanted to ask is how interleaved `and` and `or` expressions are associated when chained in a single expression, causing the minimal reproducible example of `True or True and False or False` to return `True`. So at the end this is a duplicate for https://stackoverflow.com/questions/16679272/priority-of-the-logical-operators-not-and-or-in-python – N1ngu May 27 '22 at 12:59

1 Answers1

3
z=2
y=1

y<z(True) or z>y(True) and y>z(False) or z<y(False)

True OR True AND False OR False

True AND False will be evaluated first(as AND has higher precedence over OR) ;

they come out to be False

Now, True OR True AND False OR False reduces to:

True OR False OR False

So, the final output will be True

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44