English

Explain in detail about cmp() function.

Advertisements
Advertisements

Question

Explain in detail about cmp() function.

Explain
Advertisements

Solution

  • cmp() was a Python 2 function used to compare two objects, including dictionaries and tuples.
  • Its historical form was cmp(x, y).
  • It returned:
    • 0 when x and y were equal.
    • A positive value when x was greater than y.
    • A negative value when x was less than y.
  • From the source examples: cmp(month, m) returns 0 when both dictionaries are the same; cmp(m, n) returns 1; and cmp(n, m) returns -1.
  • cmp() was removed in Python 3. Use comparison operators instead:
    • x == y for equality.
    • x > y to check whether x is greater than y.
    • x < y to check whether x is less than y.
  • A Python 3 equivalent returning -1, 0, or 1 is:
result = (x > y) - (x < y)
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 31. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×