Simple (?) question about print statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TY

    #1

    Simple (?) question about print statement

    Hi all,

    I have this little simple script:

    for i in range(10):
    for j in range(5000000): pass # Timing-delay loop
    print i

    When you run it, it behaves as you would expect -- it prints 0 <pause>
    on the next line prints 1 <pause> on the next line prints 2 <pause>
    etc.

    But if you add a comma at the end of print statement on the last line
    like this:

    for i in range(10):
    for j in range(5000000): pass # Timing-delay loop
    print i,

    Now it does this:

    <long pause> then prints 0 1 2 3 4 5 6 7 8 9 all at once.

    Why?????

    How can I make it to print each numbers on the same line with pauses in
    between them?

    Thank you for any insight!

  • Pelmen

    #2
    Re: Simple (?) question about print statement

    >From doc:

    range( [start,] stop[, step])

    This is a versatile function to create lists containing arithmetic
    progressions. It is most often used in for loops. The arguments must be
    plain integers. If the step argument is omitted, it defaults to 1. If
    the start argument is omitted, it defaults to 0. The full form returns
    a list of plain integers [start, start + step, start + 2 * step, ...].
    If step is positive, the last element is the largest start + i * step
    less than stop; if step is negative, the last element is the smallest
    start + i * step greater than stop. step must not be zero (or else
    ValueError is raised).

    Comment

    • Grant Edwards

      #3
      Re: Simple (?) question about print statement

      On 2005-12-14, TY <aqteon@yahoo.c om> wrote:
      [color=blue]
      > for i in range(10):
      > for j in range(5000000): pass # Timing-delay loop
      > print i,
      >
      > Now it does this:
      >
      ><long pause> then prints 0 1 2 3 4 5 6 7 8 9 all at once.
      >
      > Why?????
      >
      > How can I make it to print each numbers on the same line with pauses in
      > between them?[/color]

      I think there's some way to make "print" unbuffered, but I
      don't remember how to do that.

      I do do know this will work:

      for i in range(10):
      time.sleep(0.2)
      sys.stdout.writ e("%d " % i)
      sys.stdout.flus h()

      sys.stdout.writ e("\n")


      --
      Grant Edwards grante Yow! Darling, my ELBOW
      at is FLYING over FRANKFURT,
      visi.com Germany...

      Comment

      • Pelmen

        #4
        Re: Simple (?) question about print statement

        sorry ... i don'understand a question from first read
        my previos aswer is not an aswer at all

        Comment

        • Fredrik Lundh

          #5
          Re: Simple (?) question about print statement

          "TY" <aqteon@yahoo.c om> wrote:
          [color=blue]
          > I have this little simple script:
          >
          > for i in range(10):
          > for j in range(5000000): pass # Timing-delay loop
          > print i
          >
          > When you run it, it behaves as you would expect -- it prints 0 <pause>
          > on the next line prints 1 <pause> on the next line prints 2 <pause>
          > etc.
          >
          > But if you add a comma at the end of print statement on the last line
          > like this:
          >
          > for i in range(10):
          > for j in range(5000000): pass # Timing-delay loop
          > print i,
          >
          > Now it does this:
          >
          > <long pause> then prints 0 1 2 3 4 5 6 7 8 9 all at once.
          >
          > Why?????
          >
          > How can I make it to print each numbers on the same line with pauses in
          > between them?[/color]

          because stdout is line buffered.

          try this instead:

          import time, sys

          for i in range(10):
          time.sleep(0.8) # seconds; tune as necessary
          print i,
          sys.stdout.flus h() # flush stdout
          print

          </F>



          Comment

          • Grant Edwards

            #6
            Re: Simple (?) question about print statement

            On 2005-12-14, Fredrik Lundh <fredrik@python ware.com> wrote:
            [color=blue]
            > try this instead:
            >
            > import time, sys
            >
            > for i in range(10):
            > time.sleep(0.8) # seconds; tune as necessary
            > print i,
            > sys.stdout.flus h() # flush stdout
            > print[/color]

            Is mixing print and sys.stdout.XXXX X never a problem?

            I never do it because it "feels" too much like mixing printf()
            and write() on the same file descriptor. But, now that I think
            about it I doubt that's a valid analogy, and I'm probably just
            paranoid.

            --
            Grant Edwards grante Yow! The Korean War must
            at have been fun.
            visi.com

            Comment

            • Fredrik Lundh

              #7
              Re: Simple (?) question about print statement

              Grant Edwards wrote:
              [color=blue][color=green]
              > > try this instead:
              > >
              > > import time, sys
              > >
              > > for i in range(10):
              > > time.sleep(0.8) # seconds; tune as necessary
              > > print i,
              > > sys.stdout.flus h() # flush stdout
              > > print[/color]
              >
              > Is mixing print and sys.stdout.XXXX X never a problem?[/color]

              print uses sys.stdout, so the answer is no.
              [color=blue]
              > I never do it because it "feels" too much like mixing printf()
              > and write() on the same file descriptor. But, now that I think
              > about it I doubt that's a valid analogy[/color]

              it's more like mixing fprintf and fwrite.
              [color=blue]
              > and I'm probably just paranoid.[/color]

              might be, might be.

              </F>



              Comment

              • Grant Edwards

                #8
                Re: Simple (?) question about print statement

                On 2005-12-14, Fredrik Lundh <fredrik@python ware.com> wrote:[color=blue]
                > Grant Edwards wrote:
                >[color=green][color=darkred]
                >> > try this instead:
                >> >
                >> > import time, sys
                >> >
                >> > for i in range(10):
                >> > time.sleep(0.8) # seconds; tune as necessary
                >> > print i,
                >> > sys.stdout.flus h() # flush stdout
                >> > print[/color]
                >>
                >> Is mixing print and sys.stdout.XXXX X never a problem?[/color]
                >
                > print uses sys.stdout, so the answer is no.[/color]

                I presume you mean yes. After I looked into "print" a bit
                more, it was pretty obvious.

                [Asking questions in the negative like that is always bad
                style. Not sure why I did it.]

                --
                Grant Edwards grante Yow! Can I have an IMPULSE
                at ITEM instead?
                visi.com

                Comment

                • TY

                  #9
                  Re: Simple (?) question about print statement

                  So I guess then my next question is why does adding comma to print
                  statement cause buffering, but not when you don't have comma?

                  Comment

                  • Dan M

                    #10
                    Re: Simple (?) question about print statement

                    On Wed, 14 Dec 2005 15:27:58 -0800, TY wrote:
                    [color=blue]
                    > So I guess then my next question is why does adding comma to print
                    > statement cause buffering, but not when you don't have comma?[/color]

                    Because of the line buffering. If you don't have a comma at the end, the
                    print statement prints the numeric value and a newline, which signals the
                    end of the line.

                    Comment

                    Working...