Python functions-cmp (x, y)

Source: Internet
Author: User
Python function-cmp (x, y) today I will share a very useful and simple function named cmp in python. as you may have guessed in spelling, this is a comparison function, yes, it only returns three values, positive, 0, and negative. let's take a look:

Cmp (x, y)

English description: compare two objects x and y. If x <y, return a negative number; x = y, return 0; x> y, return a positive number.

Version: This function is only available in python2 and is available in all versions of python2. However, this function has been deleted in python3, so pay special attention to this.


Compare the two objects x and y and return an integer according to the outcome. the return value is negative if x <y, zero if x = y and strictly positive if x> y.


Sample code:

>>> Cmp (1, 2)-1> cmp (1, 1) 0> cmp (5, 2) 1> cmp ('abcd ', 'A') 1 # Note: At this time, it will first compare the first character, then compare the second character, one by one comparison knows that the size can be determined.

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.