How to convert a number to binary?
Sion Arrowsmith
siona at chiark.greenend.org.uk
Fri May 18 08:49:48 EDT 2007
Lyosha <lyoshaM at gmail.com> wrote:
>On May 17, 11:04 pm, Stargaming <stargam... at gmail.com> wrote:
>> dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else ''
> [ ... ]
>I guess the reason I couldn't come up with something like this was
>being brainwashed that lambda is a no-no.
>
>And python2.5 funky ?: expression comes in handy!
def dec2bin(x): return x and (dec2bin(x/2)+str(x%2)) or ''
does the same job without lambda or Python 2.5 (and note that the
usual warning about a and b or c doesn't apply here as b is
guaranteed to evaluate as true).
--
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
More information about the Python-list
mailing list