Agrupación de condiciones en Python
Aunque la operación and tiene prioridad
sobre or, a menudo es más conveniente usar
paréntesis de agrupación para mostrar explícitamente
la prioridad de las operaciones:
tst = 3
if (tst > 0 and tst < 5) or (tst > 10 and tst < 20):
print('+++')
else:
print('---')
En el código siguiente, especifique la prioridad de las operaciones de forma explícita:
tst = 3
if tst > 5 and tst < 10 or tst == 20:
print('+++')
else:
print('---')
En el código siguiente, especifique la prioridad de las operaciones de forma explícita:
tst = 3
if tst > 5 and tst > 0 and tst < 3:
print('+++')
else:
print('---')
En el código siguiente, especifique la prioridad de las operaciones de forma explícita:
tst = 3
if tst == 9 and tst > 10 and tst < 20 or tst > 20 and tst < 30:
print('+++')
else:
print('---')