Why does print() run only after executing for loop in Python?

+1 vote

Suppose I have a for loop. 

for i in range(10):
    print(i)

So now instead of printing the numbers from 0 to 9 one by one, python waits and prints them all at once. I want to know why is this happening. 

Aug 1 in Python by Arvind
• 2,760 points
14 views

1 answer to this question.

+1 vote

Your stdout is line-buffered; this means that it won't show the text until a newline is encountered. To solve this, you need to explicitly flush the buffer.

import sys
for i in range(10):
    print(i)
sys.stdout.flush()
answered Aug 1 by Neel
• 2,730 points

Related Questions In Python

0 votes
1 answer

Why there is no do while loop in python

There is no do...while loop because there ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 56,520 points
99 views
+1 vote
1 answer

Why does x,y = zip(*zip(a,b)) work in Python?

I'm extremely new to Python so this ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,520 points
55 views
0 votes
1 answer

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

answered Dec 4, 2018 in Python by Nymeria
• 3,520 points

edited Dec 6, 2018 by Nymeria 790 views
0 votes
1 answer

How to use for loop in Python?

There are multiple ways of using for ...READ MORE

answered Mar 4 in Python by Priyaj
• 56,520 points
20 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 176 views
+4 votes
6 answers
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26 in Python by Neel
• 2,730 points
42 views
0 votes
1 answer

Is it possible to print all the modules imported in a python script?

Since I am using Python 3.6, I ...READ MORE

answered Jul 3 in Python by Neel
• 2,730 points
25 views