Precedence of Comparison Operators in Python
The operation and
has priority over or
, i.e. it will be executed first in the condition.
In the following example, the condition will be triggered if the variable tst
is from 0
to 5
OR from 10
to 20
:
tst = 3
if tst > 0 and tst < 5 or tst > 10 and tst < 20:
print('+++')
else:
print('---')
The following code is given:
tst1 = 5
tst2 = -5
if tst1 > 0 and tst1 < 7 or tst2 > -10:
print('+++')
else:
print('---')
Tell me in what order the comparison is made here.
The following code is given:
tst1 = 1
tst2 = 10
if tst1 < 5 or tst2 > 9 and tst2 < 15:
print('+++')
else:
print('---')
Tell me in what order the comparison is made here.