Python Tuple – cmp() function

It is a function in python 2.x that compares two tuple provided in the argument. If the first argument tuple is greater than the second argument tuple it returns 1, if not returns -1.

Syntax:

cmp(tuple1, tuple2)

Example 1:

# Python program to understand cmp() function in tuple
# www.codewindow.in

a = (1, 2, 1)
b = (1, 2, 2)
print(cmp(a, b))

Output:

-1

Explanation:

Here the third element in the first tuple is lower than the third element in the second tuple. Hence returned -1.

Example 2:

# Python program to understand cmp() function in tuple
# www.codewindow.in

c = ('a', 'b', 'e')
d = ('a', 'b', 'd')
print(cmp(c, d))

Output:

1

Explanation:

Here the third element of the first tuple is greater than the third element of the second tuple. Hence returned 1.


You Missed

Also Checkout