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)
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)
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
.
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.