Re: Noob questions about Python

2007-10-19 Thread Grant Edwards
On 2007-10-19, Hendrik van Rooyen <[EMAIL PROTECTED]> wrote: > "Arnaud Delobelle" wrote: > >> In binary 2 is 10. When you multiply by 10, you shift all your digits >> left by 1 place. >> When you multiply by 10**n (which is 1 followed by n zeroes), you >> shift all your digits left by n places. >

Re: Noob questions about Python

2007-10-19 Thread Hendrik van Rooyen
"Arnaud Delobelle" wrote: > In binary 2 is 10. When you multiply by 10, you shift all your digits > left by 1 place. > When you multiply by 10**n (which is 1 followed by n zeroes), you > shift all your digits left by n places. I read somewhere: Only 1 person in 1000 understands binary. The oth

Re: Noob questions about Python

2007-10-19 Thread A.T.Hofkamp
On 2007-10-19, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > On Oct 19, 1:44 am, MRAB <[EMAIL PROTECTED]> wrote: >> On Oct 18, 7:05 am, Michele Simionato <[EMAIL PROTECTED]> >> >> if number == 0: >> return "0" >> > > Hey, > > Isn't > if not number: > return "0" > > faster? Depends o

Re: Noob questions about Python

2007-10-19 Thread cokofreedom
On Oct 19, 1:44 am, MRAB <[EMAIL PROTECTED]> wrote: > On Oct 18, 7:05 am, Michele Simionato <[EMAIL PROTECTED]> > wrote:> On Oct 17, 5:58 pm, Ixiaus <[EMAIL PROTECTED]> wrote: > > > > def bin2dec(val): > > > li = list(val) > > > li.reverse() > > > res = [int(li[x])*2**x for x in range(l

Re: Noob questions about Python

2007-10-18 Thread Michele Simionato
On Oct 18, 7:44 pm, MRAB <[EMAIL PROTECTED]> wrote: > 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 >

Re: Noob questions about Python

2007-10-18 Thread Ixiaus
That was a very helpful answer Steven, thank you for taking the time to write it! -- http://mail.python.org/mailman/listinfo/python-list

Re: Noob questions about Python

2007-10-18 Thread MRAB
On Oct 18, 7:05 am, Michele Simionato <[EMAIL PROTECTED]> wrote: > On Oct 17, 5:58 pm, Ixiaus <[EMAIL PROTECTED]> 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 sam

Re: Noob questions about Python

2007-10-18 Thread Steven D'Aprano
On Wed, 17 Oct 2007 23:06:24 -0700, Ixiaus wrote: > I know '<<' is shifting x over by n bits; but could you point me to some > literature that would explain why it is the same as "x*2**n"? (Googling > only returns bit shift, but doesn't necessarily explain the manner in > which you are using it)

Re: Noob questions about Python

2007-10-18 Thread Arnaud Delobelle
On Oct 18, 7:06 am, Ixiaus <[EMAIL PROTECTED]> wrote: [...] > I know '<<' is shifting x over by n bits; but could you point me to > some literature that would explain why it is the same as "x*2**n"? I haven't got literature but I've got a (hopefully straightforward) explanation: In binary 2 is 10

Re: Noob questions about Python

2007-10-18 Thread Bruno Desthuilliers
Stargaming a écrit : > On Wed, 17 Oct 2007 22:05:36 +0200, Bruno Desthuilliers wrote: > [snip] >> Note that there's also the reverse() function that returns a reverse >> iterator over any sequence, so you could also do: >> >> li = list('allo') >> print ''.join(reverse(li)) >> > > Note this certain

Re: Noob questions about Python

2007-10-18 Thread Stargaming
On Wed, 17 Oct 2007 22:05:36 +0200, Bruno Desthuilliers wrote: [snip] > > Note that there's also the reverse() function that returns a reverse > iterator over any sequence, so you could also do: > > li = list('allo') > print ''.join(reverse(li)) > Note this certainly should've been `reversed()`

Re: Noob questions about Python

2007-10-17 Thread Ixiaus
> Right idea: now to remove all those intermediate lists you construct. > 1. reversed(val) creates an iterator that runs over the elements (here > of a string) in reverse order. > 2. enumerate() is usually better than using an explicit list index. > 3. You can use a generator in your sum to avoid c

Re: Noob questions about Python

2007-10-17 Thread Michele Simionato
On Oct 17, 5:58 pm, Ixiaus <[EMAIL PROTECTED]> 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

Re: Noob questions about Python

2007-10-17 Thread George Sakkis
On Oct 17, 6:23 pm, Paul Hankin <[EMAIL PROTECTED]> wrote: > On Oct 17, 10:58 pm, Ixiaus <[EMAIL PROTECTED]> wrote: > > > > > 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

Re: Noob questions about Python

2007-10-17 Thread hg
Ixiaus wrote: > I have recently (today) just started learning/playing with Python. So > far I am excited and impressed (coming from PHP background). > > I have a few questions regarding Python behavior... > > val = 'string' > li = list(val) > print li.reverse() > > returns nothing, but, > > va

Re: Noob questions about Python

2007-10-17 Thread [EMAIL PROTECTED]
On Oct 17, 4:58 pm, Ixiaus <[EMAIL PROTECTED]> wrote: > 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 >

Re: Noob questions about Python

2007-10-17 Thread Paul Hankin
On Oct 17, 10:58 pm, Ixiaus <[EMAIL PROTECTED]> wrote: > 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

Re: Noob questions about Python

2007-10-17 Thread Ixiaus
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

Re: Noob questions about Python

2007-10-17 Thread Bjoern Schliessmann
Ixiaus wrote: > val = 'string' > li = list(val) > print li.reverse() > > returns nothing, but, Yes -- li.reverse() returns None. "print None" prints nothing. > val = 'string' > li = list(val) > li.reverse() > print li > > returns what I want. I'm afraid not. li.reverse() still returns None,

Re: Noob questions about Python

2007-10-17 Thread Neil Cerutti
On 2007-10-17, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Ixiaus a écrit : >> val = 00110 >> >> as the integer 72 instead of returning 00110, why does Python >> do that? > > Literal integers starting with '0' (zero) are treated as octal. > It's a pretty common convention (like 0x for hexa).

Re: Noob questions about Python

2007-10-17 Thread Paul Hankin
On Oct 17, 8:37 pm, Ixiaus <[EMAIL PROTECTED]> wrote: > I have a few questions regarding Python behavior... > as the integer 72 instead of returning 00110, why does Python do that? > (and how can I get around it?) You can do this: def bin(x): return int(x, 2) val = bin('00110') -- Paul Hank

Re: Noob questions about Python

2007-10-17 Thread Bruno Desthuilliers
Ixiaus a écrit : > I have recently (today) just started learning/playing with Python. So > far I am excited and impressed Welcome onboard then !-) > (coming from PHP background). > > I have a few questions regarding Python behavior... > > val = 'string' > li = list(val) > print li.reverse() >

Re: Noob questions about Python

2007-10-17 Thread Adam Atlas
On Oct 17, 3:37 pm, Ixiaus <[EMAIL PROTECTED]> wrote: > I have recently (today) just started learning/playing with Python. So > far I am excited and impressed (coming from PHP background). > > I have a few questions regarding Python behavior... > > val = 'string' > li = list(val) > print li.reverse

Re: Noob questions about Python

2007-10-17 Thread Furkan Kuru
li.reverse() does not return a list it just changes the list li. val = 00110 is evaluated in base 8 try without leading 0s On 10/17/07, Ixiaus <[EMAIL PROTECTED]> wrote: > > I have recently (today) just started learning/playing with Python. So > far I am excited and impressed (coming from PHP b

Re: Noob questions about Python

2007-10-17 Thread Grant Edwards
On 2007-10-17, Ixiaus <[EMAIL PROTECTED]> wrote: > val = 'string' > li = list(val) > print li.reverse() > > returns nothing, but, > > val = 'string' > li = list(val) > li.reverse() > print li > > returns what I want. Why does Python do that? Because it does. :) > Also I have been playing around

Noob questions about Python

2007-10-17 Thread Ixiaus
I have recently (today) just started learning/playing with Python. So far I am excited and impressed (coming from PHP background). I have a few questions regarding Python behavior... val = 'string' li = list(val) print li.reverse() returns nothing, but, val = 'string' li = list(val) li.reverse(

Noob questions about Python

2007-10-17 Thread Ixiaus
I have recently (today) just started learning/playing with Python. So far I am excited and impressed (coming from PHP background). I have a few questions regarding Python behavior... val = 'string' li = list(val) print li.reverse() returns nothing, but, val = 'string' li = list(val) li.reverse(