Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   loops (http://www.velocityreviews.com/forums/t640425-loops.html)

Gandalf 10-18-2008 09:26 AM

loops
 
how can I do width python a normal for loop width tree conditions like
for example :

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


thanks

Gandalf 10-18-2008 10:44 AM

Re: loops
 
On Oct 18, 12:39*pm, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
> Gandalf <goldn...@gmail.com> wrote:
> > how can I do width python a normal for loop width tree conditions like
> > for example :

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

>
> What you wrote would appear to be an infinite loop so I'll assume you meant
> to assign something to x each time round the loop as well. The simple
> Python translation of what I think you meant would be:
>
> x = 1
> while x <= 100:
> * *print x
> * *x += x
>
> If you really insist on doing it with a for loop:
>
> def doubling(start, limit):
> * * x = start
> * * while x <= limit:
> * * * * yield x
> * * * * x += x
>
> ...
>
> for x in doubling(1, 100):
> * * print x


thanks

Gandalf 10-18-2008 10:52 AM

Re: loops
 
On Oct 18, 12:39*pm, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
> Gandalf <goldn...@gmail.com> wrote:
> > how can I do width python a normal for loop width tree conditions like
> > for example :

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

>
> What you wrote would appear to be an infinite loop so I'll assume you meant
> to assign something to x each time round the loop as well. The simple
> Python translation of what I think you meant would be:
>
> x = 1
> while x <= 100:
> * *print x
> * *x += x
>
> If you really insist on doing it with a for loop:
>
> def doubling(start, limit):
> * * x = start
> * * while x <= limit:
> * * * * yield x
> * * * * x += x
>
> ...
>
> for x in doubling(1, 100):
> * * print x


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

robert 10-18-2008 11:24 AM

Re: loops
 
Gandalf wrote:
> On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@invalid.invalid>
> wrote:
>> Gandalf <goldn...@gmail.com> wrote:
>>> how can I do width python a normal for loop width tree conditions like
>>> for example :
>>> for x=1;x<=100;x+x:
>>> print x

>> What you wrote would appear to be an infinite loop so I'll assume you meant
>> to assign something to x each time round the loop as well. The simple
>> Python translation of what I think you meant would be:
>>
>> x = 1
>> while x <= 100:
>> print x
>> x += x
>>

...
>
> 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


You'd not save code, but only lines (and clearness). You'd also
need more (non-space) characters

Python saves confusion and arbitrariness => you'll usually code
faster, because you don't think so much about voluptuous
multimulti..possibilites, not worth the play: one-ness of mind

If insistent, you could sometimes save lines like this ;-)

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


Robert

Aaron Brady 10-18-2008 11:27 AM

Re: loops
 
Gandalf wrote:

> On Oct 18, 12:39*pm, Duncan Booth <duncan.bo...@invalid.invalid>
> wrote:
>> Gandalf <goldn...@gmail.com> wrote:
>> > how can I do width python a normal for loop width tree conditions like
>> > for example :

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

>>
>> What you wrote would appear to be an infinite loop so I'll assume you meant
>> to assign something to x each time round the loop as well. The simple
>> Python translation of what I think you meant would be:
>>
>> x = 1
>> while x <= 100:
>> * *print x
>> * *x += x
>>
>> If you really insist on doing it with a for loop:
>>
>> def doubling(start, limit):
>> * * x = start
>> * * while x <= limit:
>> * * * * yield x
>> * * * * x += x
>>
>> ...
>>
>> for x in doubling(1, 100):
>> * * print x

>
> 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


Do you anticipate reusing it? You could make something a little more
extendable.

for x in iexpression( 'x', 1, 100, 'x+x' ):
print x

or

for x in iexpression( lambda x: x+x, 1, 100 ):
print x

I'm assuming you don't want or have a closed form, in this case x= 2**
_x.


Terry Reedy 10-18-2008 06:31 PM

Re: loops
 
Gandalf wrote:
> On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@invalid.invalid>
> wrote:
>> Gandalf <goldn...@gmail.com> wrote:
>>> how can I do width python a normal for loop width tree conditions like
>>> for example :
>>> for x=1;x<=100;x+x:
>>> print x

>> What you wrote would appear to be an infinite loop so I'll assume you meant
>> to assign something to x each time round the loop as well. The simple
>> Python translation of what I think you meant would be:
>>
>> x = 1
>> while x <= 100:
>> print x
>> x += x
>>
>> If you really insist on doing it with a for loop:
>>
>> def doubling(start, limit):
>> x = start
>> while x <= limit:
>> yield x
>> x += x
>>
>> ...
>>
>> for x in doubling(1, 100):
>> print x

>
> 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


Python: 'makes common things easy and uncommon things possible'.

The large majority of use cases for iteration are iterating though
sequences, actual and virtual, including integers with a constant step
size. Python make that trivial to do and clear to read. Your example is
trivially written as

for i in range(11):
print 2**i

Python provide while loops for more fine-grain control, and a protocol
so *reuseable* iterators can plug into for loops. Duncan showed you
both. If you *need* a doubling loop variable once, you probably need
one more than once, and the cost of the doubling generator is amortized
over all such uses. Any Python proprammer should definitely know how to
write such a thing without hardly thinking. We can squeeze a line out
of this particular example:

def doubling(value, limit):
while value <= limit:
yield value
value += value

Terry Jan Reedy


wbowers 10-18-2008 07:47 PM

Re: loops
 
On Oct 18, 11:31*am, Terry Reedy <tjre...@udel.edu> wrote:
> Gandalf wrote:
> > On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@invalid.invalid>
> > wrote:
> >> Gandalf <goldn...@gmail.com> wrote:
> >>> how can I do width python a normal for loop width tree conditions like
> >>> for example :
> >>> for x=1;x<=100;x+x:
> >>> * * print x
> >> What you wrote would appear to be an infinite loop so I'll assume you meant
> >> to assign something to x each time round the loop as well. The simple
> >> Python translation of what I think you meant would be:

>
> >> x = 1
> >> while x <= 100:
> >> * *print x
> >> * *x += x

>
> >> If you really insist on doing it with a for loop:

>
> >> def doubling(start, limit):
> >> * * x = start
> >> * * while x <= limit:
> >> * * * * yield x
> >> * * * * x += x

>
> >> ...

>
> >> for x in doubling(1, 100):
> >> * * print x

>
> > 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

>
> Python: 'makes common things easy and uncommon things possible'.
>
> The large majority of use cases for iteration are iterating though
> sequences, actual and virtual, including integers with a constant step
> size. *Python make that trivial to do and clear to read. Your example is
> trivially written as
>
> for i in range(11):
> * *print 2**i
>
> Python provide while loops for more fine-grain control, and a protocol
> so *reuseable* iterators can plug into for loops. Duncan showed you
> both. *If you *need* a doubling loop variable once, you probably need
> one more than once, and the cost of the doubling generator is amortized
> over all such uses. *Any Python proprammer should definitely know how to
> write such a thing without hardly thinking. *We can squeeze a line out
> of this particular example:
>
> def doubling(value, limit):
> * *while value <= limit:
> * * *yield value
> * * *value += value
>
> Terry Jan Reedy


I agree that using range() for simple iterations is the way to go.
Here are some examples of python expressions you'd use in specific
situations:

# instead of for (i = 0; i < 100; i++)
for i in range(100): pass

# instead of for (i = 10; i < 100; i++)
for i in range(10, 100): pass

# instead of for (i = 1; i < 100; i += 2)
for i in range(1, 100, 2): pass

# instead of for (i = 99; i >= 0; i--)
for i in range(100)[::-1]: pass

There's always a way to do it, and it's almost always really simple :-D

robert 10-18-2008 08:59 PM

Re: loops
 
Aaron Brady wrote:
> Gandalf wrote:
>
>> On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@invalid.invalid>
>> wrote:
>>> Gandalf <goldn...@gmail.com> wrote:
>>>> how can I do width python a normal for loop width tree conditions like
>>>> for example :
>>>> for x=1;x<=100;x+x:
>>>> print x
>>> What you wrote would appear to be an infinite loop so I'll assume you meant
>>> to assign something to x each time round the loop as well. The simple
>>> Python translation of what I think you meant would be:
>>>
>>> x = 1
>>> while x <= 100:
>>> print x
>>> x += x
>>>
>>> If you really insist on doing it with a for loop:
>>>
>>> def doubling(start, limit):
>>> x = start
>>> while x <= limit:
>>> yield x
>>> x += x
>>>
>>> ...
>>>
>>> for x in doubling(1, 100):
>>> print x

>> 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

>
> Do you anticipate reusing it? You could make something a little more
> extendable.
>
> for x in iexpression( 'x', 1, 100, 'x+x' ):
> print x
>
> or
>
> for x in iexpression( lambda x: x+x, 1, 100 ):
> print x
>
> I'm assuming you don't want or have a closed form, in this case x= 2**
> _x.
>



#and to learn even more about this, import this:
import this # ;-)

MRAB 10-19-2008 12:04 AM

Re: loops
 
On Oct 18, 7:31*pm, Terry Reedy <tjre...@udel.edu> wrote:
> Gandalf wrote:
> > On Oct 18, 12:39 pm, Duncan Booth <duncan.bo...@invalid.invalid>
> > wrote:
> >> Gandalf <goldn...@gmail.com> wrote:
> >>> how can I do width python a normal for loop width tree conditions like
> >>> for example :
> >>> for x=1;x<=100;x+x:
> >>> * * print x
> >> What you wrote would appear to be an infinite loop so I'll assume you meant
> >> to assign something to x each time round the loop as well. The simple
> >> Python translation of what I think you meant would be:

>
> >> x = 1
> >> while x <= 100:
> >> * *print x
> >> * *x += x

>
> >> If you really insist on doing it with a for loop:

>
> >> def doubling(start, limit):
> >> * * x = start
> >> * * while x <= limit:
> >> * * * * yield x
> >> * * * * x += x

>
> >> ...

>
> >> for x in doubling(1, 100):
> >> * * print x

>
> > 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

>
> Python: 'makes common things easy and uncommon things possible'.
>
> The large majority of use cases for iteration are iterating though
> sequences, actual and virtual, including integers with a constant step
> size. *Python make that trivial to do and clear to read. Your example is
> trivially written as
>
> for i in range(11):
> * *print 2**i
>
> Python provide while loops for more fine-grain control, and a protocol
> so *reuseable* iterators can plug into for loops. Duncan showed you
> both. *If you *need* a doubling loop variable once, you probably need
> one more than once, and the cost of the doubling generator is amortized
> over all such uses. *Any Python proprammer should definitely know how to
> write such a thing without hardly thinking. *We can squeeze a line out
> of this particular example:
>
> def doubling(value, limit):
> * *while value <= limit:
> * * *yield value
> * * *value += value
>

Shouldn't the upper limit be exclusive in order to be Pythonic?

Terry Reedy 10-19-2008 03:28 AM

Re: loops
 
MRAB wrote:
> On Oct 18, 7:31 pm, Terry Reedy <tjre...@udel.edu> wrote:


>> Python provide while loops for more fine-grain control, and a protocol
>> so *reuseable* iterators can plug into for loops. Duncan showed you
>> both. If you *need* a doubling loop variable once, you probably need
>> one more than once, and the cost of the doubling generator is amortized
>> over all such uses. Any Python proprammer should definitely know how to
>> write such a thing without hardly thinking. We can squeeze a line out
>> of this particular example:
>>
>> def doubling(value, limit):
>> while value <= limit:
>> yield value
>> value += value
>>

> Shouldn't the upper limit be exclusive in order to be Pythonic?


Yes, and perhaps I could have mentioned that, but the OP wanted a port
of the C construct.



All times are GMT. The time now is 08:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57