C'è un operatore "not equal" in Python?
Come diresti che non è uguale?
Come
if hi == hi:
print "hi"
elif hi (does not equal) bye:
print "no hi"
C'è qualcosa di equivalente a ==
che significa "non uguale"?
363
3
Usare
!=
. Vedere operatori di confronto. Per confrontare le identità degli oggetti, puoi usare la parola chiaveis
e la sua negazioneis not
.ad es.
Non uguale
!=
(vs uguale==
)Stai chiedendo qualcosa di simile a questo?
Questo grafico Python - Operatori di base potrebbe essere utile.
C'è l'operatore
!=
(not equal) che restituisceTrue
quando due valori differiscono, anche se bisogna fare attenzione ai tipi perché"1" != 1
. Questo restituirà sempre True e"1" == 1
restituirà sempre False, poiché i tipi differiscono. Python è dinamicamente, ma fortemente tipizzato, e altri linguaggi staticamente tipizzati si lamenterebbero di confrontare tipi diversi.C'è anche la clausola
else
:L'operatore
is
è l'operatore di identità dell'oggetto usato per controllare se due oggetti sono effettivamente uguali: