Noob questions about Python
cokofreedom at gmail.com
cokofreedom at gmail.com
Fri Oct 19 04:01:52 EDT 2007
On Oct 19, 1:44 am, MRAB <goo... at mrabarnett.plus.com> wrote:
> On Oct 18, 7:05 am, Michele Simionato <michele.simion... at gmail.com>
> wrote:> On Oct 17, 5:58 pm, Ixiaus <parnel... at comcast.net> wrote:
>
> > > 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!
>
> > BTW, here is the reverse function dec2bin, so that you can
> > bang your head on it for a while ;)
>
> It returns '' when number == 0, so you need to test for that case:
>
> > def baseN(number, N=2):
> > """
> > >>> baseN(9, 2)
> > '1001'
> > """
> > assert 2 <= N <= 10
> > assert isinstance(number, int) and number >= 0
>
> if number == 0:
> return "0"
>
Hey,
Isn't
if not number:
return "0"
faster?
More information about the Python-list
mailing list