loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven D'Aprano

    #16
    Re: loops

    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 .auwrote:
    [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

    Comment

    • John Machin

      #17
      Re: loops



      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 .auwrote:
      [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".
      >

      Comment

      • Steven D'Aprano

        #18
        Re: loops

        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 .auwrote:
        [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

        Comment

        Working...