473,545 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1521
>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.c om> 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.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...
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.c om> 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.flus h() # flush stdout
print

</F>

Dec 14 '05 #5
On 2005-12-14, Fredrik Lundh <fr*****@python ware.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.flus h() # flush stdout
print


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
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.flus h() # flush stdout
print
Is mixing print and sys.stdout.XXXX X 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*****@python ware.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.flus h() # flush stdout
> print


Is mixing print and sys.stdout.XXXX X 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
6958
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 have tried a few things but no joy what am I missing. Is the another snippet in relation to get_payload to access the body contents print and...
27
2347
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. It is available at: http://staff.washington.edu/sabbey/py_do Good background on thunks can be found in ref. . Simple Thunks
15
3851
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 completing its work formB issues docmd.close ,Me docmd.openform "formA"
5
7227
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, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline...
73
4543
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 endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix...
14
5879
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
1307
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 have boiled my code down to the bare essentials in the hope that someone can help me with this problem. I would be grateful if you could explain...
4
1233
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 = test.testcase(name=config.get(section,'name')) 3 tests.runTest() 4 printStatus(tests)
1
4223
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
7390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7649
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7410
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5960
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3437
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
692
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.