
December 14th, 2005, 08:55 PM
| | | 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! | 
December 14th, 2005, 09:05 PM
| | | 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). | 
December 14th, 2005, 09:05 PM
| | | Re: Simple (?) question about print statement
On 2005-12-14, TY <aqteon@yahoo.com> 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.write("%d " % i)
sys.stdout.flush()
sys.stdout.write("\n")
--
Grant Edwards grante Yow! Darling, my ELBOW
at is FLYING over FRANKFURT,
visi.com Germany... | 
December 14th, 2005, 09:05 PM
| | | Re: Simple (?) question about print statement
sorry ... i don'understand a question from first read
my previos aswer is not an aswer at all | 
December 14th, 2005, 09:15 PM
| | | Re: Simple (?) question about print statement
"TY" <aqteon@yahoo.com> 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.flush() # flush stdout
print
</F> | 
December 14th, 2005, 09:25 PM
| | | Re: Simple (?) question about print statement
On 2005-12-14, Fredrik Lundh <fredrik@pythonware.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.flush() # flush stdout
> print[/color]
Is mixing print and sys.stdout.XXXXX 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 | 
December 14th, 2005, 09:45 PM
| | | 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.flush() # flush stdout
> > print[/color]
>
> Is mixing print and sys.stdout.XXXXX 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> | 
December 14th, 2005, 09:55 PM
| | | Re: Simple (?) question about print statement
On 2005-12-14, Fredrik Lundh <fredrik@pythonware.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.flush() # flush stdout
>> > print[/color]
>>
>> Is mixing print and sys.stdout.XXXXX 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 | 
December 14th, 2005, 11:35 PM
| | | 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? | 
December 14th, 2005, 11:55 PM
| | | 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. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|