Latest web development tutorials

Python Tuple (tuple) cmp () method

description

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

grammar

cmp () method syntax:

cmp(tuple1, tuple2)

parameter

  • 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.

Examples

The following example shows cmp () function to use:

#!/usr/bin/python

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:

-1
1
-1