Priorità degli operatori di confronto in Python
L'operatore and ha priorità sull'operatore
or, cioè verrà eseguito per primo
all'interno della condizione.
Nel seguente esempio, la condizione si attiverà
se la variabile tst è compresa tra 0 e
5 OPPURE tra 10 e 20:
tst = 3
if tst > 0 and tst < 5 or tst > 10 and tst < 20:
print('+++')
else:
print('---')
È dato il seguente codice:
tst1 = 5
tst2 = -5
if tst1 > 0 and tst1 < 7 or tst2 > -10:
print('+++')
else:
print('---')
Indicare in quale ordine vengono eseguiti i confronti.
È dato il seguente codice:
tst1 = 1
tst2 = 10
if tst1 < 5 or tst2 > 9 and tst2 < 15:
print('+++')
else:
print('---')
Indicare in quale ordine vengono eseguiti i confronti.