Latest web development tutorials

Python cmp () function

Python figures Python figures


description

cmp (x, y) function is used to compare two objects, if x <y -1, returns 0 if x == y, if x> y returns 1.


grammar

The following is the syntax cmp () method:

cmp( x, y )

parameter

  • x - numeric expression.
  • y - numeric expression.

return value

If x <y returns -1 if x == y Returns 0 if x> y returns 1.

Examples

The following shows an example of the use of cmp () method:

#!/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)

After running the above example output is:

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

Python figures Python figures