newbie question

Discussion in 'Python' started by doodle4@gmail.com, Feb 9, 2005.

  1. Guest

    Hello All,
    What is the python equivalent of the following statement?

    while (n--)

    Thank you.
    -d4
     
    , Feb 9, 2005
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    > What is the python equivalent of the following statement?
    >
    > while (n--)
    >
    > Thank you.


    That depends on a number of things, including whether "n"
    is declared as "volatile", and whether you consider the
    above as an executable piece of code (it is not), or just a
    code snippet...

    Roughly but somewhat facetiously speaking, the answer is this:

    n = -1

    (Maybe describing what you are trying to accomplish would
    be more useful than showing snippets of code without
    explaining what you are expecting it to do.)

    -Peter
     
    Peter Hansen, Feb 9, 2005
    #2
    1. Advertising

  3. Guest

    Thanks for the reply.

    I am trying to convert some C code to python and i was not sure what
    the equivalent python code would be.

    I want to postdecrement the value in the while loop. Since i cannot use
    assignment in while statements is there any other way to do it in
    python?

    Thanks.
    -d4
     
    , Feb 9, 2005
    #3
  4. Peter Hansen Guest

    wrote:
    > Thanks for the reply.
    >
    > I am trying to convert some C code to python and i was not sure what
    > the equivalent python code would be.
    >
    > I want to postdecrement the value in the while loop. Since i cannot use
    > assignment in while statements is there any other way to do it in
    > python?


    Generally in Python you simply put the assignment-based
    statement in a "while True:" block, and use "break" to
    exit as required. For example, here is one way to
    approach it in this case:

    while True:
    n = n - 1
    if n == -1:
    break

    Having written the above, however, I'm uncertain whether this
    sort of thing is really required in Python. Although I have
    ported very little C code to Python (so I might be wrong), I
    definitely have never seen anything resembling the above
    pattern in Python code that I or others have written.

    My suspicion is that the code involves operations which are
    actually better handled in Python by some kind of builtin
    or standard library operation. For example, often the
    post-decrementing is part of a pointer operation involving
    manipulating a string character-by-character. This is
    effectively never required in Python, and will pretty much
    always result in a program written at a much lower level
    than what you should be shooting for in Python. But if
    you want a direct port, then the above should do...

    -Peter
     
    Peter Hansen, Feb 9, 2005
    #4
  5. Dan Perl Guest

    <> wrote in message
    news:...
    > Hello All,
    > What is the python equivalent of the following statement?
    >
    > while (n--)


    Like other posters said, you should give more details with your question.
    What do you mean by equivalent? The following is *functionally* equivalent:
    for n in range(start, 0, -1):

    Of course, there is always:
    while n:
    ...
    n -= 1

    But unfortunately, no, I don't think there is an equivalent in python that
    has the conciseness of the C statement. The pre- and post-increment
    and -decrement in C/C++/Java are very powerful and I miss them in python.
     
    Dan Perl, Feb 10, 2005
    #5
  6. Jeff Shannon Guest

    wrote:

    > Thanks for the reply.
    >
    > I am trying to convert some C code to python and i was not sure what
    > the equivalent python code would be.
    >
    > I want to postdecrement the value in the while loop. Since i cannot use
    > assignment in while statements is there any other way to do it in
    > python?


    Roughly speaking, if you have a loop in C like

    while (n--) {
    func(n);
    }

    then the mechanical translation to Python would look like this:

    while True:
    n -= 1
    if not n:
    break
    func(n)

    However, odds are fairly decent that a mechanical translation is not
    the best approach, and you may (as just one of many examples) be much
    better off with something more like:

    for i in range(n)[::-1]:
    func(n)

    The '[::-1]' iterates over the range in a reverse (decreasing)
    direction; this may or may not be necessary depending on the
    circumstances.

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Feb 10, 2005
    #6
  7. In article <>,
    Dan Perl <> wrote:
    .
    .
    .
    >has the conciseness of the C statement. The pre- and post-increment
    >and -decrement in C/C++/Java are very powerful and I miss them in python.
    >
    >


    Me, too.

    Which is, I suspect, evidence for the incompleteness of our Pythonhood.
    As Peter Hansen already hinted in this thread, an appetite for the
    increment and related operators probably is a symptom that there's an
    opportunity nearby to use an iterator or string method or such. C++
    and Java wish they had it so good.
     
    Cameron Laird, Feb 10, 2005
    #7
  8. Dan Perl Guest

    "Cameron Laird" <> wrote in message
    news:...
    > In article <>,
    > Dan Perl <> wrote:
    > .
    > .
    > .
    >>has the conciseness of the C statement. The pre- and post-increment
    >>and -decrement in C/C++/Java are very powerful and I miss them in python.

    >
    > Me, too.
    >
    > Which is, I suspect, evidence for the incompleteness of our Pythonhood.
    > As Peter Hansen already hinted in this thread, an appetite for the
    > increment and related operators probably is a symptom that there's an
    > opportunity nearby to use an iterator or string method or such. C++
    > and Java wish they had it so good.


    I can't say that is not part of the reason, but the example in the OP is a
    clear illustration of cases where something like an increment/decrement
    operator would be very useful. OTOH, I was thinking of saying in my
    previous posting that I prefer
    for n in range(start, 0, -1):
    to
    n = start
    while (n--)
    I think that the first form is more readable, although that may be just me.
    I would actually even prefer the 'for' statement in C to the 'while'
    statement:
    for (n=start; n<=0; n--)
     
    Dan Perl, Feb 10, 2005
    #8
  9. On Wed, 09 Feb 2005 18:10:40 -0800, Jeff Shannon <>
    declaimed the following in comp.lang.python:

    >
    > for i in range(n)[::-1]:
    > func(n)


    Shouldn't that be
    func(i)
    (the loop index?)

    >
    > The '[::-1]' iterates over the range in a reverse (decreasing)
    > direction; this may or may not be necessary depending on the
    > circumstances.
    >

    Eeee.... sneaky... (I'm a bit behind on latest syntax additions)

    I'd probably have coded something like

    for n1 in range(n):
    func(n-n1)

    though, and note that I do admit it here, I haven't done the detailed
    analysis to properly set the end point... Let's see... n initialized at
    3, while condition sees:

    pre post function
    3 2 2
    2 1 1
    1 0 0
    0 exits

    My code sees: range(3) gives 0, 1, 2

    n1 func
    0 3-0->3
    1 3-1->2
    2 3-2->1
    exits

    Hmm, need

    func(n-1-n1)

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 10, 2005
    #9
  10. Peter Hansen Guest

    Dan Perl wrote:
    > I can't say that is not part of the reason, but the example in the OP is a
    > clear illustration of cases where something like an increment/decrement
    > operator would be very useful.


    The OP didn't show how he was using the "while (n--)" at all,
    so it can hardly be a clear illustration of how it's useful.
    In fact, it's even possible it was entirely unnecessary in
    the original code... at this point I'd be really interested
    in seeing just what code is inside the "while" statement, and
    possibly what follows it (if the following code relies on the
    value of "n").

    > OTOH, I was thinking of saying in my
    > previous posting that I prefer
    > for n in range(start, 0, -1):
    > to
    > n = start
    > while (n--)
    > I think that the first form is more readable, although that may be just me.
    > I would actually even prefer the 'for' statement in C to the 'while'
    > statement:
    > for (n=start; n<=0; n--)


    I'm not sure if it's just picking nits, but I'd like to
    point out that neither of your alternatives is actually
    equivalent to the while (n--) form... nor was Jeff
    Shannon's attempt (in that case it leaves the loop with
    n equal to 0, not -1).

    The fact that it's so easy to get confused with post-decrement
    is perhaps an excellent reason to keep it out of Python.

    -Peter
     
    Peter Hansen, Feb 10, 2005
    #10
  11. Dan Perl Guest

    "Peter Hansen" <> wrote in message
    news:...
    > Dan Perl wrote:
    >> OTOH, I was thinking of saying in my previous posting that I prefer
    >> for n in range(start, 0, -1):
    >> to
    >> n = start
    >> while (n--)
    >> I think that the first form is more readable, although that may be just
    >> me. I would actually even prefer the 'for' statement in C to the 'while'
    >> statement:
    >> for (n=start; n<=0; n--)

    >
    > I'm not sure if it's just picking nits, but I'd like to
    > point out that neither of your alternatives is actually
    > equivalent to the while (n--) form... nor was Jeff
    > Shannon's attempt (in that case it leaves the loop with
    > n equal to 0, not -1).


    You're right in your nit picking. I have to go back to using some C/C++ and
    Java also, I don't want to forget them completely.
     
    Dan Perl, Feb 10, 2005
    #11
  12. Jeff Shannon Guest

    Dennis Lee Bieber wrote:

    > On Wed, 09 Feb 2005 18:10:40 -0800, Jeff Shannon <>
    > declaimed the following in comp.lang.python:
    >
    >
    >>for i in range(n)[::-1]:
    >> func(n)

    >
    >
    > Shouldn't that be
    > func(i)
    > (the loop index?)


    You're right, that's what I *meant* to say. (What, the interpreter
    doesn't have a "do what I mean" mode yet? ;) )

    >>The '[::-1]' iterates over the range in a reverse (decreasing)
    >>direction; this may or may not be necessary depending on the
    >>circumstances.

    >
    > Eeee.... sneaky... (I'm a bit behind on latest syntax additions)
    >
    > I'd probably have coded something like
    >
    > for n1 in range(n):
    > func(n-n1)
    >
    > though, and note that I do admit it here [...]


    Given a need/desire to avoid extended slicing (i.e. being stuck with
    an older Python, as I often am), I'd actually do this by changing the
    input to range(), i.e.

    for i in range(n, 0, -1): # ...

    That (IMO) makes the decreasing-integer sequence a bit clearer than
    doing subtraction in the function parameter list does. Actually, it's
    possibly clearer than the extended slicing, too, so maybe this would
    be the better way all around... ;)

    > I haven't done the detailed
    > analysis to properly set the end point...


    And as Peter Hansen points out, none of the Python versions leave n in
    the same state that the C loop does, so that's one more way in which
    an exact translation is not really possible -- and (IMO again) further
    evidence that trying to do an exact translation would be
    ill-conceived. Much better to consider the context in which the loop
    is used and do a looser, idiomatic translation.

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Feb 10, 2005
    #12
  13. On Thu, 10 Feb 2005 09:36:42 -0800, Jeff Shannon <>
    declaimed the following in comp.lang.python:

    > And as Peter Hansen points out, none of the Python versions leave n in
    > the same state that the C loop does, so that's one more way in which
    > an exact translation is not really possible -- and (IMO again) further
    > evidence that trying to do an exact translation would be
    > ill-conceived. Much better to consider the context in which the loop
    > is used and do a looser, idiomatic translation.
    >

    Yeah, though my background tends to be one which considers loop
    indices to be loop-local, value indeterminate after exit...

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 11, 2005
    #13
  14. Jeff Shannon Guest

    Dennis Lee Bieber wrote:
    > On Thu, 10 Feb 2005 09:36:42 -0800, Jeff Shannon <>
    > declaimed the following in comp.lang.python:
    >
    >>And as Peter Hansen points out, none of the Python versions leave n in
    >>the same state that the C loop does, so that's one more way in which
    >>an exact translation is not really possible -- and (IMO again) further
    >>evidence that trying to do an exact translation would be
    >>ill-conceived. Much better to consider the context in which the loop
    >>is used and do a looser, idiomatic translation.

    >
    > Yeah, though my background tends to be one which considers loop
    > indices to be loop-local, value indeterminate after exit...


    Well, even though I've programmed mostly in langauges where loop
    indices to retain a determinate value after exit, I almost always
    *treat* them as loop-local -- it just seems safer that way. But not
    everyone does so, and especially with C while loops, often the point
    is to keep adjusting the control variable until it fits the
    requirements of the next section...

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Feb 11, 2005
    #14
  15. John Machin Guest

    Dan Perl wrote:
    > <> wrote in message
    > news:...
    > > Hello All,
    > > What is the python equivalent of the following statement?
    > >
    > > while (n--)

    >
    > Like other posters said, you should give more details with your

    question.
    > What do you mean by equivalent? The following is *functionally*

    equivalent:
    > for n in range(start, 0, -1):
    >
    > Of course, there is always:
    > while n:
    > ...
    > n -= 1


    errrmmm it's a C while stmt with post-decrement; that's NOT the same
    as:

    for (; n; n--) ...

    If n is initially 3, the loop body is executed with n set to 2,1,0 and
    after exit n is -1.

    The nearest Python equivalents of "while (n--) func(n);" are
    (making the charitable assumption that initially n >= 0):

    ! while n:
    ! n -= 1
    ! func(n)

    and

    ! for n in range(n-1, -1, -1):
    ! func(n)

    >
    > But unfortunately, no, I don't think there is an equivalent in python

    that
    > has the conciseness of the C statement. The pre- and post-increment
    > and -decrement in C/C++/Java are very powerful and I miss them in

    python.

    Unfortunately?? What are you missing? The ability to write cryptic crap
    that you don't understand & that's so "powerful" that you can get into
    deep kaka really quickly? Hope you weren't writing software for
    embedded devices -- like heart pacemakers :)
     
    John Machin, Feb 11, 2005
    #15
  16. On Fri, 11 Feb 2005 12:07:34 -0800, Jeff Shannon <>
    declaimed the following in comp.lang.python:


    > *treat* them as loop-local -- it just seems safer that way. But not
    > everyone does so, and especially with C while loops, often the point
    > is to keep adjusting the control variable until it fits the
    > requirements of the next section...
    >

    Lacking that context in the sample, I naturally fell into the
    "discard it after loop" mode <G>

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 12, 2005
    #16
    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. Jerry C.
    Replies:
    8
    Views:
    252
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    147
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    128
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    115
  5. Miki Tebeka
    Replies:
    0
    Views:
    86
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page