Simple (?) question about print statement

T

TY

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!
 
P

Pelmen

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).
 
G

Grant Edwards

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?

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")
 
P

Pelmen

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

Fredrik Lundh

TY said:
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?

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>
 
G

Grant Edwards

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

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.
 
F

Fredrik Lundh

Grant said:
Is mixing print and sys.stdout.XXXXX never a problem?

print uses sys.stdout, so the answer is no.
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

it's more like mixing fprintf and fwrite.
and I'm probably just paranoid.

might be, might be.

</F>
 
G

Grant Edwards

print uses sys.stdout, so the answer is no.

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.]
 
T

TY

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?
 
D

Dan M

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?

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top