Skip to content
Related Articles

Related Articles

Improve Article
Python | cmp() function
  • Difficulty Level : Basic
  • Last Updated : 24 Apr, 2020

cmp() method in Python 2.x compares two integers and returns -1, 0, 1 according to comparison.
cmp() does not work in python 3.x. You might want to see list comparison in Python.

cmp(a, b)
a and b are the two numbers in which the comparison is being done. 
-1 if a<b

0 if a=b

1 if a>b

# Python program to demonstrate the 
# use of cmp() method
# when a<b
a = 1 
b = 2 
print(cmp(a, b))  
# when a = b 
a = 2
b = 2 
print(cmp(a, b))  
# when a>b 
a = 3
b = 2 
print(cmp(a, b))



Practical Application: Program to check if a number is even or odd using cmp function.

Approach: Compare 0 and n%2, if it returns 0, then it is even, else its odd.

Below is the Python implementation of the above program:

# Python program to check if a number is  
# odd or even using cmp function  
# check 12  
n = 12 
if cmp(0, n % 2):  
# check 13     
n = 13 
if cmp(0, n % 2):  



 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :