- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does the cmp() function do in Python Object Oriented Programming?
The cmp() function
The cmp(x,y) function compares the values of two arguments x and y −
cmp(x, y)
The return value is −
A negative number if x is less than y.
Zero if x is equal to y.
A positive number if x is greater than y.
The built-in cmp() function will typically return only the values -1, 0, or 1. However, there are other places that expect functions with the same calling sequence, and those functions may return other values. It is best to observe only the sign of the result.
>>> cmp(2,8) -1 >>> cmp(6,6) 0 >>> cmp(4,1) 1 >>> cmp('stackexchange', 'stackoverflow') -1
The method cmp() compares elements of two lists.
Syntax
cmp(list1, list2)
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 lists, the longer list is "larger." If we exhaust both lists 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.
list1, list2 = [456, 'xyz'], [789, 'abc'] print cmp(list1, list2) print cmp(list2, list1) list3 = list2 + [896]; print cmp(list2, list3)
Output
When we run above program, it produces following result −
-1 1 -1
- Related Articles
- What does the repr() function do in Python Object Oriented Programming?
- What does the str() function do in Python Object Oriented Programming?
- Object Oriented Programming in Python?
- What is Data Hiding in Python Object Oriented Programming?
- Python Object-oriented and Functional Programming
- What is object-oriented programming (OOP)?
- What is the difference between Object oriented programming and Object based programming?
- What are basic Object oriented programming concepts?
- What is an Object Oriented Programming in JavaScript?
- Object Oriented language v/s Procedure oriented programming language.
- How to start object-oriented programming in C++?
- Object Oriented language v/s Object based programming language.
- Basic Concepts of Object Oriented Programming using C++
- What does reload() function do in Python?
- What does raw_input() function do in python?
