[Tutor] [newbie alert] why can't I find a function that gives me the sign of an integer?

Kent Johnson kent37 at tds.net
Thu Jan 26 21:30:04 CET 2006


Orri Ganel wrote:
> Rinzwind wrote:
> 
>> In basic I can use SGN to get back -1, 0,  +1 if a number is <0, 0, >0.
> Well, the cmp() function does this if you compare the number to 0:
> 
>  >>> cmp(-34,0)
> -1
>  >>> cmp(0,0)
> 0
>  >>> cmp(23,0)

One caution: this behaviour doesn't seem to be required, the docs allow 
any positive or negative integer:
cmp(  	x, y)
     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.

So this behaviour might differ between Python versions.

Kent



More information about the Tutor mailing list