Noob questions about Python

Ixiaus parnell.s at comcast.net
Wed Oct 17 17:58:36 EDT 2007


Thank you for the quick responses.

I did not know that about integer literals beginning with a '0', so
thank you for the explanation. I never really use PHP except for
handling basic forms and silly web stuff, this is why I picked up
Python because I want to teach myself a more powerful and broad
programming language.

With regard to why I asked: I wanted to learn about Binary math in
conjunction with Python, so I wrote a small function that would return
a base 10 number from a binary number. It is nice to know about the
int() function now.

Just for the sake of it, this was the function I came up with:

def bin2dec(val):
    li = list(val)
    li.reverse()
    res = [int(li[x])*2**x for x in range(len(li))]
    res.reverse()
    print sum(res)

Now that I look at it, I probably don't need that last reverse()
because addition is commutative...

def bin2dec(val):
    li = list(val)
    li.reverse()
    res = [int(li[x])*2**x for x in range(len(li))]
    print sum(res)

It basically does the same thing int(string, 2) does.

Thank you for the responses!




More information about the Python-list mailing list