Tutorials Point


  Python Basics
  Python Advanced
  Python Useful References
  Python Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Python Tuple cmp() Function


previous next AddThis Social Bookmark Button


Description:

This function compares elements of two tuples.

Syntax:

cmp(tuple1, tuple2)

Parameters:

Here is the detail of parameters:

  • tuple1: first tuple to be compared

  • tuple2: second tuple to be compared

Return Value:

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 numbers, perform numeric coercion if necessary and compare.
  • If either element is a number, then the other element is "larger" (numbers are "smallest").
  • Otherwise, types are sorted alphabetically by name.

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.

Example:

#!/usr/bin/python

tuple1, tuple2 = (123, 'xyz'), (456, 'abc')

print cmp(tuple1, tuple2);
print cmp(tuple2, tuple1);
tuple3 = tuple2 + (786,);
print cmp(tuple2, tuple3)

This will produce following result:

-1
1
-1


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names