Binary numbers

Steve Holden sholden at holdenweb.com
Tue Feb 6 12:44:00 EST 2001


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:M3Wf6.34454$o5.418224 at e420r-atl1.usenetserver.com...
> <sampe99 at my-deja.com> wrote in message news:95p7ks$9e6$1 at nnrp1.deja.com...
> > Does anyone know an easy way to get a list of binary numbers within a
> > certain range in python? E.g for
> > n=1 [0],[1]
> > n=2 [0,0],[0,1],[1,0],[1,1]
> > n=3 [0,0,0],[0,0,1] a.s.o
> >
> > I need this for n<=18...
> >
> > Thanks a lot in advance
> >
> > /Sam
> >
> Maybe this will help:
>
In my enthusiasm to present a tidy code listing I appear to have indented
one statement a little further than I should. I hate mailers that mess with
my layout! The function should have been:

def bin(n):
    l = []
    while n > 0:
        l[:0] = [n%2]
        n /= 2
    if l == [] : return [0]
    return l

regards
 Steve





More information about the Python-list mailing list