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.