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]
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
Again:
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.