[Tutor] Function for converting ints from base10 to base2?
Brian van den Broek
broek at cc.umanitoba.ca
Thu Feb 22 03:40:00 CET 2007
Dick Moores said unto the world upon 02/21/2007 08:08 PM:
> At 05:17 PM 2/21/2007, Terry Carroll wrote:
<snip>
>> I like the approach of mapping hex or octal digits posted by Alan and Bob,
>> but, not thinking of that, this would be my straightforward approach:
>>
>> def computeBin(n):
>> """converts base10 integer n to base2 b as string"""
>> if n == 0: return '0'
>> sign = ['','-'][n<0]
<snip>
>
> Thanks!
>
> But there's syntax(?) there I've never seen before. "['','-'][n<0]".
> I see it works:
>
> >>> n = -6
> >>> ['','-'][n<0]
> '-'
> >>> n = 98
> >>> ['','-'][n<0]
> ''
>
> What's this called? I'd like to look it up.
Hi Dick and all,
I don't know that it has a name other than `trickery!' ;-)
But, if you're scratching your head over it:
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1==True
True
>>> 0==False
True
>>>
Best,
Brian vdB
More information about the Tutor
mailing list