user6142489
user6142489

Reputation: 532

Python Print Order

This is probably a dumb question, but I'm new to programming and I have a recursive function set up that I'm trying to figure out. For any print function in Python, is it necessarily true that lines are printed in the order that they are written in the script OR for larger outputs, is it possible that smaller length outputs can get printed first in the console even though the print statement is later in the code (maybe due to some memory lag)?

Example:

 def test_print():
      #don't run this, but was meant for scale. Is there any chance the 1 would print before the list of lists?
      print([[i for i in range(10000)] for j in range(10000)])
      print(1)

Upvotes: 0

Views: 5594

Answers (3)

Gideon Kimutai
Gideon Kimutai

Reputation: 156

As said above, print() function is executed in the order which they are in your code. But you yourself can change the order in which you want it executed, after all you have every right to instruct the code to do whatever you want.

Upvotes: 1

Abhishek Kasireddy
Abhishek Kasireddy

Reputation: 370

Print statements pile output into stdout in the order the code was written. Top to bottom. It isn't possible any other way because that's the way the code is interpreted. Memory lag doesn't play any role here because the output to your console is a line for line rendition of the data that was piled into stdout. And the order the data was written to it can't change, so you'll maintain chronology. Of course, you can always play around with the how the print function itself works. But I wouldn't recommend tampering with standard library functions.

Upvotes: 1

viraptor
viraptor

Reputation: 34145

You'll always get the same order in the output as the order you execute print() functions in Python.

Upvotes: 0

Related Questions