Python Tuple cmp() Method



The Python Tuple cmp() method is used to compare the elements of two tuples.

The elements of tuples can be of same or different data types. If elements are of the same type, compare them and return the result. If elements are different types, check to see if they are numbers.

There are two different cases in the comparison done by this method. They are as follows −

Case 1: Suppose the tuple contains only numbers, the method compares elements from left to right. If a greater number is encountered in a tuple, the tuple containing it is declared greater and the further comparisons are halted. But if all the elements are equal, the tuples are declared equal.

Case 2: If there are multiple data types in the tuple, the method compares it based on the order these data types and are sorted. For example, a string type is greater than any number type, hence, if a tuple contains a string, it is greater.

Note: This method is only executable in Python 2.x and does not work in Python 3.x.

Syntax

Following is the syntax of the Python Tuple cmp() method −

cmp(tuple1, tuple2)

Parameters

  • tuple1 − This is the first tuple to be compared

  • tuple2 − This is the second tuple to be compared

Return Value

If elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.

  • If numbers, perform numeric coercion if necessary and compare.

  • If either element is a number, then the other element is "larger" (numbers are "smallest").

  • Otherwise, types are sorted alphabetically by name.

If we reached the end of one of the tuples, the longer tuple is "larger." If we exhaust both tuples and share the same data, the result is a tie, meaning that 0 is returned.

Example

The following example shows the usage of Python Tuple cmp() method. This program executes only in Python 2.x versions.

tuple1, tuple2 = (123, 'xyz'), (456, 'abc')
print cmp(tuple1, tuple2)
print cmp(tuple2, tuple1)
tuple3 = tuple2 + (786,);
print cmp(tuple2, tuple3)

When we run above program, it produces following result −

-1
1
-1

Example

If the user wants to execute this method in the Python 3.x interpreter, they can declare a user-defined function as shown in the given example. This method works similar to the built-in cmp() method.

def cmp(x, y):
   return (x > y) - (x < y)
#x > y
x = (123)
y = (112)
print("The cmp value for x>y is : ",cmp(x, y),"\n")
#x<y
x = (123, 'xyz')
y = (456, 'abc')
print("The cmp value for x<y is : ",cmp(x, y),"\n")
#x=y
x = (76)
y = (76)
print("The cmp value for x=y is : ",cmp(x, y))
#odd and even
k = 16
if cmp(0, k % 2):
   print("\n","The given number",k,"is odd number ")
else:
   print("\n","The given number",k,"is even number")
k= 31
if cmp(0, k % 2):
   print("\n","The given number",k,"is odd number")
else:
   print("\n","The given number",k,"is even number")

Running the above code gives us the following result −

The cmp value for x>y is :  1 

The cmp value for x<y is :  -1 

The cmp value for x=y is :  0

 The given number 16 is even number

 The given number 31 is odd number
python_tuples.htm
Advertisements