Python cmp () function

Source: Internet
Author: User

Describe

The CMP (x, y) function is used to compare 2 objects if x < y returns 1 if x = = y returns 0 if x > y returns 1.

Grammar

The following is the syntax for the CMP () method:
CMP (x, y)

Parameters

X--Numeric expression.
Y--numeric expression.

return value

If x < y returns 1, if x = = y returns 0, if x > y returns 1.

Instance

The following shows an example of using the CMP () method:

# !/usr/bin/python Print "  ", CMP (+)print", CMP (+/-) Print "  ", CMP ( -80, +)print", CMP (80,-100 )

When the above instance is run, the output is:

CMP (+):  -1cmp(+):  1cmp (-80,):  -1cmp (+ -100):  1

There are no CMP functions in the Python 3.X version, and if you need to implement the Compare function, you need to introduce the operator module, which is suitable for any object, including the following methods:

operator.lt (A, B) operator.le ( A, b) Operator.eq (A, B) Operator.ne (A, B) operator.ge (A, B) operator.gt (A, b) operator. __lt__ (A, B) operator. __le__ (A, B) operator. __eq__ (A, B) operator. __ne__ (A, B) operator. __ge__ (A, B) operator. __gt__ (A, B)

Instance

Import operator>>> operator.eq ('hello''name' ) ); False>>> operator.eq ('Hello' 'Hello' ); True

Python cmp () function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.