Vitalynx Vitalynx - 4 months ago 52
Python Question

Order of Evaluation in Python

k = 10 / 3
l = k <= 3 or True

The value for l in this code returns True. How is this possible? In the order of evaluation, comparisons are higher prioritised than the "or, not, and" operators.


They do have a higher priority, but, the or operator works by:

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

Since k is 3.3333333333333335, the expression k <= 3 is False, True is going to be evaluated (to itself) and returned making l == True.