Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python cmp() function

Python cmp() function

Python Numbers Python Numbers


Description

The cmp(x,y) function compares two objects; if x < y returns -1, if x == y returns 0, if x > y returns 1.


Syntax

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


cmp( x, y )

Parameters


Return Value

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


Example

The following example shows 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

)

Running the example above produces:


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

Python Numbers Python Numbers