Python cmp() Function

Python 数字Python Numbers


Description

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


Syntax

The following is the syntax of the cmp() method:

cmp( x, y )

Parameters

  • x -- Numeric expression.
  • y -- Numeric expression.

Return Value

Returns -1 if x < y, 0 if x == y, and 1 if x > y.

Example

The following example demonstrates the use of the cmp() method:

Example

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

The output after running the above example is:

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

Python 数字Python Numbers

Other Extensions