cmp() function - Python Last Updated : 19 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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.Example Python # Example 1 print(cmp(3, 4)) # Example 2 print(cmp(5, 5)) # Example 3 print(cmp(7, 6)) Output-101ExplanationIn the first example, 3 is smaller than 4, so the function returns -1.In the second example, 5 is equal to 5, so it returns 0.In the third example, 7 is greater than 6, so it returns 1.Note:The cmp function was a built-in function in Python 2 for comparing the values of two objects. It has been removed in Python 3 and replaced with the == and is operators, which provide more robust and flexible comparison functionality.Syntax of cmp() functioncmp(x, y)Parametersx: The first value to compare.y: The second value to compare.Return Type0 if x == y (both are equal).1 if x > y (first value is greater).-1 if x < y (first value is smaller).Example of cmp() functionChecking if a Number is Even or OddProgram 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 # Python program to check if a number is # odd or even using cmp function # check 12 n = 12 if cmp(0, n % 2): print"odd" else: print"even" # check 13 n = 13 if cmp(0, n % 2): print"odd" else: print"even" OutputevenoddExplanationThe cmp() function was used to compare 0 and n % 2 to check if the number is even or odd.In Python 3, you can directly use comparison operators like == to achieve the same result without using cmp(). Comment More infoAdvertise with us Next Article cmp() function - Python R Rahul_ Follow Improve Article Tags : Misc Python Python-Built-in-functions Practice Tags : Miscpython Similar Reads Python compile() Function Python is a high-level, general-purpose, and very popular programming language. In this article, we will learn about the Python compile() function. Python compile() Function SyntaxPython compile() function takes source code as input and returns a code object that is ready to be executed and which ca 3 min read Python | fmod() function fmod() function is one of the Standard math library function in Python, which is used to calculate the Module of the specified given arguments. Syntax: math.fmod( x, y ) Parameters: x any valid number (positive or negative). y any valid number(positive or negative). Returns: Return a floating point 2 min read Python Functions Python Functions is a block of statements that return the specific task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it ov 11 min read chr() Function in Python chr() function returns a string representing a character whose Unicode code point is the integer specified. chr() Example: Python3 num = 97 print("ASCII Value of 97 is: ", chr(num)) OutputASCII Value of 97 is: a Python chr() Function Syntaxchr(num) Parametersnum: an Unicode code integerRet 3 min read Python - cmath.cos() function cMath module contains a number of functions which is used for mathematical operations for complex numbers. The cmath.cos() function returns the cosine value of a complex number. The value passed in this function can be int, float, and complex numbers. Syntax: cmath.cos(x) Parameter:This method accep 1 min read Python - cmath.sin() function cmath is Python built-in module that is used for complex number mathematics. cmath module has a method sin() that returns sine of the complex number passed to it. Syntax: cmath.sin(Z) Parameter: It requires only one parameter i.e the number for which sine needs to be calculated. Return: Returns a co 1 min read Python - cmath.tan() function cmath is Python built-in module that is used for complex number mathematics. cmath module has a method tan() that returns tangent of the complex number passed to it. Syntax: cmath.tan(Z) Parameter: It requires only one parameter i.e the number for which tangent needs to be calculated. Return: Return 1 min read Python - cmath.acos() function cMath module contains a number of functions which is used for mathematical operations for complex numbers. The cmath.acos() function returns the arc cosine value of a complex number. The value passed in this function can be int, float, and complex numbers. Syntax: cmath.acos(x) Parameter:This method 1 min read Python - cmath.cosh() function cMath module contains a number of functions which is used for mathematical operations for complex numbers. The cmath.cosh() function returns the hyperbolic cosine value of a complex number. The value passed in this function can be int, float, and complex numbers. Syntax: cmath.cosh(x) Parameter:This 1 min read Python - cmath.atan() function cMath module contains a number of functions which is used for mathematical operations for complex numbers. The cmath.atan() function returns the arctangent value of a complex number. The value passed in this function can be int, float, and complex numbers. Syntax: cmath.atan(x) Parameter:This method 1 min read Like