Latest web development tutorials

Python Tuple (tuple) cmp () method


Python tuple cmp () function is used to compare two tuples of elements.


cmp () method syntax:

cmp(tuple1, tuple2)


  • tuple1 - Comparison of tuples.
  • tuple2 - Compare another tuple.

return value

If you compare the elements of the same type, then compare its value, returning the result.

If the two elements are not the same type, check whether they are digital.

  • If it is digital, perform the necessary digital casts, and then compare.
  • If there is one element of a number, the other elements of the "big" (the number is the "smallest")
  • Otherwise, type the name alphabetically by comparison.

If there is a list of the first to reach the end, the other a longer list of "big."

If we run out of elements of the two lists and all the elements are equal, then the result is a draw, that returns a 0.


The following example shows cmp () function to use:


tuple1, tuple2 = (123, 'xyz'), (456, 'abc')

print cmp(tuple1, tuple2);
print cmp(tuple2, tuple1);
tuple3 = tuple2 + (786,);
print cmp(tuple2, tuple3)

Examples of the above output results are as follows: