How to convert a number to binary?

Lyosha lyoshaM at gmail.com
Fri May 18 03:45:08 EDT 2007


On May 17, 11:04 pm, Stargaming <stargam... at gmail.com> wrote:
[...]
> >>>Is there an *easy* way to convert a number to binary?
[...]
>
> Wrote this a few moons ago::
>
>    dec2bin = lambda x: (dec2bin(x/2) + str(x%2)) if x else ''

This is awesome.  Exactly what I was looking for.  Works for other
bases too.

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!

Thanks a lot!




More information about the Python-list mailing list