Change language

# What does the cmp () function do in OOP in Python?

|

## The cmp () function

The cmp (x, y) function compares the values ​​of two arguments x and y:

cm (x, y)

Returned value:

Negative number if x is less than y.

Zero if x equals y.

A positive number if x is greater than y.

The built-in cmp () function usually returns only -1 values , 0, or 1. However, there are other places where functions with the same calling sequence are expected, and these functions may return different values. It is best to observe only the result sign.

`""" cmp (2.8) -1""" cmp (6.6) 0""" cmp (4.1) 1""" cmp (’stackexchange’,’ stackoverflow’) -1 `

The cmp () method compares the elements of two lists.

Syntax

cmp (list1, list2)

If elements are of the same type, do the comparison and return the result. If the elements are of different types, check if they are numbers.

If numbers, cast numerically if necessary and compare.

If one of the elements is a number, then the other is " large "(the numbers are the" smallest ").

Otherwise, the types are sorted alphabetically by name.

If we reach the end of one of the lists, the longer list will be“ larger ". If we exhaust both lists and use the same data, the result is equality, which means that 0 is returned.

example

The following example shows the use of the cmp () method.

` list1, list2 = [456, ’xyz’], [789,’ abc’] print cmp (list1, list2) print cmp ( list2, list1) list3 = list2 + [896]; print cmp (list2, list3) `

When we run the above program, it produces the following output &

` - 1 1 -1 `

## Shop

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Best laptop for Zoom

\$499

Best laptop for Minecraft

\$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method