Describe
The CMP (x, y) function is used to compare 2 objects if x < y returns 1 if x = = y returns 0 if x > y returns 1.
Grammar
The following is the syntax for the CMP () method:
CMP (x, y)
Parameters
X--Numeric expression.
Y--numeric expression.
return value
If x < y returns 1, if x = = y returns 0, if x > y returns 1.
Instance
The following shows an example of using the CMP () method:
# !/usr/bin/python Print " ", CMP (+)print", CMP (+/-) Print " ", CMP ( -80, +)print", CMP (80,-100 )
When the above instance is run, the output is:
CMP (+): -1cmp(+): 1cmp (-80,): -1cmp (+ -100): 1
There are no CMP functions in the Python 3.X version, and if you need to implement the Compare function, you need to introduce the operator module, which is suitable for any object, including the following methods:
operator.lt (A, B) operator.le ( A, b) Operator.eq (A, B) Operator.ne (A, B) operator.ge (A, B) operator.gt (A, b) operator. __lt__ (A, B) operator. __le__ (A, B) operator. __eq__ (A, B) operator. __ne__ (A, B) operator. __ge__ (A, B) operator. __gt__ (A, B)
Instance
Import operator>>> operator.eq ('hello''name' ) ); False>>> operator.eq ('Hello' 'Hello' ); True
Python cmp () function