cmp() Function in Python

The cmp() function in Python compares two objects and returns their values. It was a built-in function in Python 2. However, in Python 3, it has been replaced by == and is operators, which make comparison objects more robust, accurate, and flexible and return negative and positive values, and zero occurs based on comparison values. Let's find out more about that.

cmp() Function in Python

If the first integer is less than the second integer, the output will be -1. Similarly, if the first object is greater than the second, the result will be 1. If both objects are the same, the output will be 0. Let us understand the concept of this cmp() method using relevant examples.

Syntax

Parameters

Suppose x and y are the two objects in which the comparison is made. The objects can be of any data type, such as numbers or strings.

Returning Values

After comparing two numbers, it returns:

  1. If a>b, returns 1.
  2. If a<b; returns -1.
  3. If a=b; returns 0.

Some Examples to Compare Two Integers

In the following section, we will look at some examples demonstrating the method of comparing two integers in Python.

Example 1:

Output

-1
0 
1

Explanation

In the above code, the code compares two object's values using the `cmp()` method:

In the first scenario, if x is less than y, the result will be -1. In the second scenario, if the comparison of x is equal to y, it prints 0. In the third scenario, compare if x is greater than y: output will be 1.

Example 2:

Output:

5 is less than 10
12 is equal to 12
20 is greater than 18

Explanation:

We created a comp_num() function in the abovementioned program to compare two numbers. The first statement compares 5 and 10 and displays the output. The second statement compares 12 and 12 and prints the value. For the third and last call, it compares 20 and 18 and generates the result.

Check if the Number is Even or Odd using cmp()

In the following section, we will discuss how to check if the number is even or odd with the help of the cmp() method.

Example 1:

Output:

Even
Odd

Explanation:

In the first case, n%2 equals 0, indicating it's even. The cmp() function compares 0 with the result; if it's considered false, it prints "Even". In the second case, n%2 equals 1. It's odd.

Sorting a List of Tuples

We will understand how to sort a list of Tuples with the help of an example shown below.

Example:

Output:

[(5, 1), (3, 5), (2, 6), (4, 7)]

Explanation:

The above code sorts a list of tuples based on the second element of each tuple using the sorted() function with a custom comparison function defined using lambda. However, this code is for Python 2.x because it uses the cmp parameter removed in Python 3.x. The output sorts the tuples in ascending order based on their second elements.

Compare Two Strings

We will now see an example demonstrating the comparison of two strings using the cmp() method.

Example:

Output:

-1
1
0

Explanation:

In the above program, in the first case, Hello comes before Python based on alphabetical order; therefore, the output will be -1. Similarly, in the second case, the output will be 1, and in the third case, you will get an output of 0 by comparing the strings based on alphabetical order.

Comparing Lists

In the following section, we will look at some examples demonstrating the method of comparing lists in Python.

Example:

Output:

-1
1
0

Explanation:

The above code compares two lists element-wise using the cmp() method. The first print statement compares list1 with list2. It will print the -1. Similarly, the second print statement compares list2 with list1 and will print 1. The third print statement compares list1 with itself and prints 0 because they are equal.

Custom Object Comparison

Suppose we have a custom class person with two attributes: name and age. Now, we will use cmp() to sort a Person object list based on age.

Example:

Output:

John (20 years old)
Doe (22 years old)
Mac (25 years old)

Explanation:

The above code created a Person class with name and age attributes. Then, it sorts a list of Person objects based on their ages using the sorting_ppl function and will print their names and ages.

cmp() function in Sorting Algorithms

To get the order of objects in a sorting method, the cmp() function is frequently utilized. Let us take an example:

Example:

Output:

["kiwi", "pear", "peach", "papaya"]

Explanation:

In the above program, the fruits are sorted in ascending order based on the length of their names. If two fruits are of the same length, their order remains unchanged.

Conclusion

The cmp() function, once present in Python 2, facilitated value comparisons. However, it was deprecated in Python 3. Instead, Python 3 encourages comparison operators (==, !=, <, >, etc.) for more versatile comparisons. Migrating code from Python 2 to 3 necessitates replacing cmp() with modern alternatives.