Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
2.7k views
in Computer by (22.7k points)
Explain cmp(tuplel, tuple2) with example.

Please log in or register to answer this question.

1 Answer

0 votes
by (28.2k points)
cmp(tuple1, tuple2)

Despription :

The method cmp( ) compares elements of two tuples.

Syntax :

Following is the syntax for cmp( ) method :

cmp (tuplel, tuple2)

Parameters :

 tuple 1 – This is the first tuple to be compared

 tuple 2 – This is the 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 conversion 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 :

The following example shows the usage of cmp( ) method

#!/user/bin/python

tuple1, tuple2 = (123, ‘xyz’), (456, ‘abc’)

print cmp(tuple1, tuple2);

print cmp(tuple1, tuple1);

tuple3 = tuple2 + (786,);

print cmp (tuple2, tuple3)

Let us compile and run the above program, this will produce the following result :

-1

1

-1

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...