Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > loops

Reply
Thread Tools

loops

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-19-2008
On Sat, 18 Oct 2008 03:52:51 -0700, Gandalf wrote:

> I was hopping to describe it with only one command. most of the
> languages I know use this.
> It seems weird to me their is no such thing in python. it's not that I
> can't fined a solution it's all about saving code


It shouldn't be about saving code. There's no shortage of code so that we
have to conserve it. But there is a shortage of time and effort, so
making your code easy to read and easy to maintain is far more important.

for x in (2**i for i in xrange(10)):
print x

will also print 1, 2, 4, 8, ... up to 1000.



--
Steven
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      10-19-2008
On Sun, Oct 19, 2008 at 1:30 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> for x in (2**i for i in xrange(10)):
> print x


This is by far the most concise solution I've seen so far.
And it should never be about conserving code.
Also, Python IS NOT C (to be more specific: Python
is not a C-class language).

--JamesMills

--
--
-- "Problems are solved by method"
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      10-19-2008
On Sun, Oct 19, 2008 at 1:44 PM, James Mills
<(E-Mail Removed)> wrote:
> On Sun, Oct 19, 2008 at 1:30 PM, Steven D'Aprano
> <(E-Mail Removed)> wrote:
>> for x in (2**i for i in xrange(10)):
>> print x

>
> This is by far the most concise solution I've seen so far.
> And it should never be about conserving code.
> Also, Python IS NOT C (to be more specific: Python
> is not a C-class language).


Also, if the OP is finding himself writing such manual
and mundane looking loops, he/she should reconsider
what it is he/she is doing. You would normally want
to iterate (vs. loop) over a sequence of items.

--JamesMills

--
--
-- "Problems are solved by method"
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      10-19-2008
On Oct 19, 2:30*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
[snip]
> making your code easy to read and easy to maintain is far more important.
>
> for x in (2**i for i in xrange(10)):
> * * print x
>
> will also print 1, 2, 4, 8, ... up to 1000.


I would say up to 512; perhaps your understanding of "up to" differs
from mine.

Easy to read? I'd suggest this:

for i in xrange(10):
print 2 ** i

Cheers,
John
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      10-19-2008
"James Mills" <(E-Mail Removed)> writes:
> > for x in (2**i for i in xrange(10)):
> > print x

> This is by far the most concise solution I've seen so far.


print '\n'.join(str(2**i) for i in xrange(10))
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-19-2008
On Sat, 18 Oct 2008 20:45:47 -0700, John Machin wrote:

> On Oct 19, 2:30*pm, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
> [snip]
>> making your code easy to read and easy to maintain is far more
>> important.
>>
>> for x in (2**i for i in xrange(10)):
>> * * print x
>>
>> will also print 1, 2, 4, 8, ... up to 1000.

>
> I would say up to 512; perhaps your understanding of "up to" differs
> from mine.


Well, mine is based on Python's half-open semantics: "up to" 1000 doesn't
include 1000, and the highest power of 2 less than 1000 is 512.

Perhaps you meant "up to and including 512".



> Easy to read? I'd suggest this:
>
> for i in xrange(10):
> print 2 ** i



Well, sure, if you want to do it the right way *wink*.

But seriously, no, that doesn't answer the OP's question. Look at his
original code (which I assume is C-like pseudo-code):

for x=1;x<=100;x+x:
print x

The loop variable i takes the values 1, 2, 4, 8, etc. That's what my code
does. If he was asking how to write the following in Python, your answer
would be appropriate:

for x=1;x<=100;x++:
print 2**x



--
Steven
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      10-19-2008


Steven D'Aprano wrote:

> On Sat, 18 Oct 2008 20:45:47 -0700, John Machin wrote:
>
> > On Oct 19, 2:30*pm, Steven D'Aprano <st...@REMOVE-THIS-
> > cybersource.com.au> wrote:
> > [snip]
> >> making your code easy to read and easy to maintain is far more
> >> important.
> >>
> >> for x in (2**i for i in xrange(10)):
> >> * * print x
> >>
> >> will also print 1, 2, 4, 8, ... up to 1000.

> >
> > I would say up to 512; perhaps your understanding of "up to" differs
> > from mine.

>
> Well, mine is based on Python's half-open semantics: "up to" 1000 doesn't
> include 1000, and the highest power of 2 less than 1000 is 512.


We're talking about an English sentence, not a piece of Python code.
When you say "I'm taking the train to X", do you get off at the
station before X, as in "getting off at Redfern"?


>
> Perhaps you meant "up to and including 512".
>

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-19-2008
On Sun, 19 Oct 2008 03:17:51 -0700, John Machin wrote:

> Steven D'Aprano wrote:
>
>> On Sat, 18 Oct 2008 20:45:47 -0700, John Machin wrote:
>>
>> > On Oct 19, 2:30*pm, Steven D'Aprano <st...@REMOVE-THIS-
>> > cybersource.com.au> wrote:
>> > [snip]
>> >> making your code easy to read and easy to maintain is far more
>> >> important.
>> >>
>> >> for x in (2**i for i in xrange(10)):
>> >> * * print x
>> >>
>> >> will also print 1, 2, 4, 8, ... up to 1000.
>> >
>> > I would say up to 512; perhaps your understanding of "up to" differs
>> > from mine.

>>
>> Well, mine is based on Python's half-open semantics: "up to" 1000
>> doesn't include 1000, and the highest power of 2 less than 1000 is 512.

>
> We're talking about an English sentence, not a piece of Python code.
> When you say "I'm taking the train to X", do you get off at the station
> before X, as in "getting off at Redfern"?


But I don't say "I'm taking the train UP TO X".

Intervals in English are often ambiguous, which is why people often
explicitly say "up to and including...". But in this specific case, I
don't see why you're having difficulty. Whether 1000 was included or not
makes no difference, because 1000 is not a power of 2.


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple For Loops? pinod01@sympatico.ca VHDL 1 02-22-2006 03:10 PM
Etherchannel disabled = spanning tree loops... traust Cisco 0 02-21-2006 05:34 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Perl loops should use break, not last Jeremy Morton Perl 1 01-30-2005 10:50 PM
to many FOR loops? eismaus4 VHDL 1 04-27-2004 02:54 PM



Advertisments