Python Tuple cmp() Method
Description
The Python Tuple cmp() function is used to compare two tuple elements.
Syntax
cmp() method syntax:
cmp(tuple1, tuple2)
Parameters
- tuple1 -- The tuple to compare.
- tuple2 -- The other tuple to compare.
Return Value
If the elements being compared are of the same type, compare their values and return the result.
If the two elements are not of the same type, check whether they are numbers.
- If they are numbers, perform the necessary numeric coercion and then compare.
- If one of the elements is a number, then the other element is "greater" (numbers are the "smallest").
- Otherwise, compare by the alphabetical order of the type names.
If one list reaches the end first, the other longer list is "greater".
If we exhaust the elements of both lists and all elements are equal, then the result is a tie, that is, 0 is returned.
Example
The following example demonstrates the use of the cmp() function:
#!/usr/bin/python tuple1, tuple2 = (123, 'xyz'), (456, 'abc') print cmp(tuple1, tuple2); print cmp(tuple2, tuple1); tuple3 = tuple2 + (786,); print cmp(tuple2, tuple3) tuple4 = (123, 'xyz') print cmp(tuple1, tuple4)
The output of the above example is as follows:
-1 1 -1 0Other Extensions
Python Tuple