Python cmp() function
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
- x -- a numeric expression.
- y -- a numeric expression.
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
"cmp(80, 100) : "
,
cmp
(
80
,
100
)
"cmp(180, 100) : "
,
cmp
(
180
,
100
)
"cmp(-80, 100) : "
,
cmp
(
80
,
100
)
"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