loops

Discussion in 'Python' started by Gandalf, Oct 18, 2008.

  1. Gandalf

    Gandalf Guest

    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, Oct 18, 2008
    #1
    1. Advertising

  2. Gandalf

    Gandalf Guest

    On Oct 18, 12:39 pm, Duncan Booth <>
    wrote:
    > Gandalf <> 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, Oct 18, 2008
    #2
    1. Advertising

  3. Gandalf

    Gandalf Guest

    On Oct 18, 12:39 pm, Duncan Booth <>
    wrote:
    > Gandalf <> 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
     
    Gandalf, Oct 18, 2008
    #3
  4. Gandalf

    robert Guest

    Gandalf wrote:
    > On Oct 18, 12:39 pm, Duncan Booth <>
    > wrote:
    >> Gandalf <> 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
     
    robert, Oct 18, 2008
    #4
  5. Gandalf

    Aaron Brady Guest

    Gandalf wrote:

    > On Oct 18, 12:39 pm, Duncan Booth <>
    > wrote:
    >> Gandalf <> 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.
     
    Aaron Brady, Oct 18, 2008
    #5
  6. Gandalf

    Terry Reedy Guest

    Gandalf wrote:
    > On Oct 18, 12:39 pm, Duncan Booth <>
    > wrote:
    >> Gandalf <> 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
     
    Terry Reedy, Oct 18, 2008
    #6
  7. Gandalf

    wbowers Guest

    On Oct 18, 11:31 am, Terry Reedy <> wrote:
    > Gandalf wrote:
    > > On Oct 18, 12:39 pm, Duncan Booth <>
    > > wrote:
    > >> Gandalf <> 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
     
    wbowers, Oct 18, 2008
    #7
  8. Gandalf

    robert Guest

    Aaron Brady wrote:
    > Gandalf wrote:
    >
    >> On Oct 18, 12:39 pm, Duncan Booth <>
    >> wrote:
    >>> Gandalf <> 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 # ;-)
     
    robert, Oct 18, 2008
    #8
  9. Gandalf

    MRAB Guest

    On Oct 18, 7:31 pm, Terry Reedy <> wrote:
    > Gandalf wrote:
    > > On Oct 18, 12:39 pm, Duncan Booth <>
    > > wrote:
    > >> Gandalf <> 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?
     
    MRAB, Oct 19, 2008
    #9
  10. Gandalf

    Terry Reedy Guest

    MRAB wrote:
    > On Oct 18, 7:31 pm, Terry Reedy <> 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.
     
    Terry Reedy, Oct 19, 2008
    #10
  11. 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
     
    Steven D'Aprano, Oct 19, 2008
    #11
  12. Gandalf

    James Mills Guest

    On Sun, Oct 19, 2008 at 1:30 PM, Steven D'Aprano
    <> 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"
     
    James Mills, Oct 19, 2008
    #12
  13. Gandalf

    James Mills Guest

    On Sun, Oct 19, 2008 at 1:44 PM, James Mills
    <> wrote:
    > On Sun, Oct 19, 2008 at 1:30 PM, Steven D'Aprano
    > <> 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"
     
    James Mills, Oct 19, 2008
    #13
  14. Gandalf

    John Machin Guest

    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
     
    John Machin, Oct 19, 2008
    #14
  15. Gandalf

    Paul Rubin Guest

    "James Mills" <> 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))
     
    Paul Rubin, Oct 19, 2008
    #15
  16. 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
     
    Steven D'Aprano, Oct 19, 2008
    #16
  17. Gandalf

    John Machin Guest

    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".
    >
     
    John Machin, Oct 19, 2008
    #17
  18. 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
     
    Steven D'Aprano, Oct 19, 2008
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eismaus4

    to many FOR loops?

    eismaus4, Apr 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    702
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,182
    Big and Blue
    Jan 30, 2005
  3. Multiple For Loops?

    , Feb 22, 2006, in forum: VHDL
    Replies:
    1
    Views:
    494
    john Doef
    Feb 22, 2006
  4. =?Utf-8?B?Um9iIE8nQ29w?=

    RedirectFromLoginPage and loops

    =?Utf-8?B?Um9iIE8nQ29w?=, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    421
    =?Utf-8?B?Um9iIE8nQ29w?=
    Oct 25, 2004
  5. Me
    Replies:
    2
    Views:
    267
Loading...

Share This Page