How to use cmp() function in Python

In this tutorial, we will learn how to use cmp() i.e compare function in Python. This function has different behaviour in different versions of Python. In Python version 2.x (x=1,2,3,…) we can directly use cmp()  to compare any two entities of float, integer or string type.

cmp(): Compare takes two arguments on which we want to perform compare operation and it returns an integer value according to result.
It returns -1 if the first argument is less than the second, it returns 1 if the first argument is greater than the second and it returns 0 if both the arguments are equal. This cmp() function works only on Python version 2.x, if you try to use it in version 3.x it will give an error:

NameError: name 'cmp' is not defined
[Finished in 0.1s with exit code 1]

Example

See the code below:

a=60
b=90
print(cmp(a,b))
output:
-1

While comparing integers cmp() just performs subtraction of its argument i.e in this case a-b, if subtraction is -ve it returns -1 i.e a<b
if subtraction is +ve it returns 1 i.e a>b

a=90
b=60
print(cmp(a,b))
output:
1

a="abc"
b="abc"
print(cmp(a,b))
output:
0

when both the arguments are equal i.e a=b, it returns 0 as output. Here, we have passed two string type of values. Here, cmp() compares both the strings character by character and if found same then returns 0.

Comparing objects in 3.x Python versions

The cmp() function has a drawback which was removed in later versions of Python i.e 3.x(x=1,2,3,…). Whenever we try to pass an integer and a string for comparison it would always show string greater than an integer.

a="abc"
b=90
print(cmp(a,b))
print(cmp(a,b))
output:
1
-1

whenever string and integer are compared comparison is performed based on the names of the data types. By default, a string is always considered greater than an integer (i.e “str”>”int”).

So to eradicate this flaw, from version 3.x onwards Python removed cmp() function. In the operator module Python has introduced three new functions. These functions are eq() i.e equal to,lt() i.e less than and gt() i.e greater than. As their names suggest, perform a separate comparison for less than, greater than and equal to operation.
To use these functions in Python, you must have Python 3.x version installed in your system.  These functions return a boolean value as an output.

import operator as op
a='abc'
b='abc'
print(op.lt(a,b))
print(op.eq(a,b))
print(op.gt(a,b))
output:
False
True
False

As you can see we have passed two string arguments to the function and the functions have returned a boolean value according to the result.
These functions can’t compare a string with any numeric data type thus it removes the error occurring in cmp() function.

import operator as op
a='abc'
b='abc'
print(op.lt(a,b),end=" ")
print(op.gt(a,b),end=" ")
print(op.eq(a,b))

a=78
b=56
print(op.lt(a,b),end=" ")
print(op.gt(a,b),end=" ")
print(op.eq(a,b))

a=9.23
b=8
print(op.lt(a,b),end=" ")
print(op.gt(a,b),end=" ")
print(op.eq(a,b))
output:
False False True
False True False
False True False

we can observe how these functions work and perform comparison according to our need.

Also, read: Concept of Functional Programming in Python

 

Leave a Reply

Your email address will not be published. Required fields are marked *