Python tuple method cmp() compares elements of two tuples.
Following is the syntax for cmp() method −
cmp(tuple1, tuple2)
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 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.
The following example shows the usage of cmp() method.
#!/usr/bin/python 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