Latest web development tutorials

Python cmp function ()

figure Python figure Python


descrizione

la funzione cmp (x, y) viene utilizzato per confrontare due oggetti, se x <y -1, restituisce 0 se x == y, se x> y restituisce 1.


grammatica

Quanto segue è il metodo sintassi cmp ():

cmp( x, y )

parametri

  • x - espressione numerica.
  • y - espressione numerica.

Valore di ritorno

Se x <y restituisce -1 se x == y restituisce 0 se x> y restituisce 1.

Esempi

Quanto segue mostra un esempio dell'uso di cmp () Metodo:

#!/usr/bin/python

print "cmp(80, 100) : ", cmp(80, 100)
print "cmp(180, 100) : ", cmp(180, 100)
print "cmp(-80, 100) : ", cmp(-80, 100)
print "cmp(80, -100) : ", cmp(80, -100)

Dopo aver eseguito l'output sopra esempio è:

cmp(80, 100) :  -1
cmp(180, 100) :  1
cmp(-80, 100) :  -1
cmp(80, -100) :  1

figure Python figure Python