473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple (?) question about print statement

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!

Dec 14 '05 #1
9 1510
>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).

Dec 14 '05 #2
On 2005-12-14, TY <aq****@yahoo.com> wrote:
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")
--
Grant Edwards grante Yow! Darling, my ELBOW
at is FLYING over FRANKFURT,
visi.com Germany...
Dec 14 '05 #3
sorry ... i don'understand a question from first read
my previos aswer is not an aswer at all

Dec 14 '05 #4
"TY" <aq****@yahoo.com> wrote:
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>

Dec 14 '05 #5
On 2005-12-14, Fredrik Lundh <fr*****@pythonware.com> wrote:
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.

--
Grant Edwards grante Yow! The Korean War must
at have been fun.
visi.com
Dec 14 '05 #6
Grant Edwards wrote:
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?


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>

Dec 14 '05 #7
On 2005-12-14, Fredrik Lundh <fr*****@pythonware.com> wrote:
Grant Edwards wrote:
> 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?


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

--
Grant Edwards grante Yow! Can I have an IMPULSE
at ITEM instead?
visi.com
Dec 14 '05 #8
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?

Dec 14 '05 #9
On Wed, 14 Dec 2005 15:27:58 -0800, TY wrote:
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.

Dec 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
15
by: Thelma Lubkin | last post by:
formA determines some_where and some_value and issues docmd.close ,Me docmd.openform "formB", , ,some_where, , ,some_value formB receives the correct some_where and some_value After...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
4
by: ii2o | last post by:
Hi all, I'm trying to develop a website that selects from 3 stylesheets depending on which style they have chosen. Their preference is stored in a cookie and this is where the problems begin. I...
4
by: amit.uttam | last post by:
Hey there, I have a simple question about python print statement. Take the following code snippet for example... 1 print "-#- executing: %s" % section, 2 tests =...
1
by: RC | last post by:
By default the print statement sends to stdout I want to send to stderr Try print "my meeage", file=sys.stderr I got I try
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.