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.
>
"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
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
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
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
>
That was a very helpful answer Steven, thank you for taking the time
to write it!
--
http://mail.python.org/mailman/listinfo/python-list
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
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)
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
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
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()`
> 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
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
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
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
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
>
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
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
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,
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).
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
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()
>
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
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
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
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(
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(
27 matches
Mail list logo