Tutorial on using cmp () in Python2.x
This article describes how to use the cmp () method in Python2.x. In Python3.x, this method is no longer embedded. For more information, see
The cmp () method returns the difference between two numbers:-1 if x <y, 0 if x = y, or 1 if x> y.
Syntax
The syntax of the cmp () method is as follows:
?
Parameters
X -- this is a numeric expression.
Y -- this is also a numeric expression
Return Value
This method if x
Example
The following example shows the use of the cmp () method.
?
| 1 2 3 4 5 6 |
#! /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) |
When we run the above program, it will produce the following results:
?
| 1 2 3 4 |
Cmp (1, 80,100):-1 Cmp (1, 180,100): 1 Cmp (-80,100):-1 Cmp (80,-100): 1 |