Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Tuple cmp() method

Python Tuple cmp() method

Python TuplePython Tuple


Description

The Python Tuple cmp() function compares two tuple elements.

Syntax

cmp() method syntax:


cmp(tuple1, tuple2)

Parameters

Return Value

If the compared elements are of the same type, their values are compared and the result is returned.

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

If one list reaches its end first, the other, longer list is considered greater.

If we exhaust the elements of both lists and all elements are equal, the result is a tie, that is, 0 is returned.

Example

The following example shows how to use 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:


-1
1
-1
0

Python TuplePython Tuple