Python loop outputs to console

python for loop
types of loops in python
python loops explained
python print to console
what is i in python
python not printing to terminal
python print not working

Why does running this code in the interactive console print output?

>>> def a():
...   return 1
>>> for i in range(3):
...   a()

I would expect no output. Where is this behaviour documented?

In interactive mode, the result of any expression-statement is printed if it is not None. As stated in the doc.

Expression statements are used (mostly interactively) to compute and write a value [...]

In interactive mode, if the value is not None, it is converted to a string using the built-in repr() function and the resulting string is written to standard output


>>> 1 + 1
>>> a()
>>> None # This expression is not printed

This is true for a expression statement in a loop as well.

>>> for i in range(3):
>>>     a()

Although, this is specific to the interactive shell. If you run you code as a script, nothing will be printed.

Python: How to Print Without Newline or Space?, How do you print a loop on the same line in Python? In technical terms the string was written to standard output, usually the console. Python reserves two file objects for console output: standard output and standard error: >>> import sys >>> sys . stdout < _io .

If you use the console/interactive prompt/IDLE/shell/Python shell/potato, the values returned would be printed, and no print() required. This is why you can do >>> 1+1 in the shell and it will say 2, even though if you create a program with 1+1 it will run with no errors or output.

And a little detail: the value is not printed, its repr is. This is why in the shell this happens:

>>> print("something")
>>> "something"
>>> 'a string with "quotes"'
'a string with "quotes"'

You can do a quick test of this:

>>> class Test:
...     def __str__(self): return "str"
...     def __repr__(self): return "repr"

>>> test_instance = Test()
>>> test_instance
>>> print(test_instance)

How to print without newline in Python?, How do you write on the same line in Python? Writing Output to the Console. In addition to obtaining data from the user, a program will also usually need to present data back to the user. You can display program data to the console in Python with print(). Unformatted Console Output. To display objects to the console, pass them as a comma-separated list of argument to print().

This behaviour is documented here; you are running the Python shell in interactive mode.

Note, quoted below for posterity:

All error messages are written to the standard error stream; normal output from executed commands is written to standard output.

Python: How to Print Without Newline? (The Idiomatic Way), How do you print on the same line in Python 3? How to output to console with print function Output to console. Printing a line of text in Python.

command line - Python terminal not working for for loops, . Whatever you provide as the end argument is going to be the terminating string. Console Input/Output in Python: Console Input/Output output is most important as it acts as the basic text based interface for the python programs. Almost all of us who learn programming start with writing a program that prints to the screen - "Hello World". Console output in Python: The built-in function print() of Python, prints any string

code — Interpreter base classes, It doesn't matter what data he is working on. Either way you're just using print "​spam" or some complicated expression, the for-loop should be  I don't know if anyone knows how to do this, or even if this is the right place to ask this. But how would one clear the output console using the print() function in python? I haven't the slightest clue how where to start with this question, but i do have the complete python reference for python 3.3.0 and yes, i do use python 3.3.0

Python2 Tutorial: While Loops, The code module provides facilities to implement read-eval-print loops in Python. This creates a new instance of InteractiveConsole and sets readfunc to be used as Derived classes should override this to provide the appropriate output​  Fancier Output Formatting¶ So far we’ve encountered two ways of writing values: expression statements and the print() function. (A third way is using the write() method of file objects; the standard output file can be referenced as sys.stdout. See the Library Reference for more information on this.)

  • What are you confused about? The fact that you get output to the console? It should not do so without print statements
  • i ran your script, and it is giving me no output.
  • why would you expect something different. you are running the same function 3 times
  • Just to confirm, by "console" you mean "interactive prompt", right? The thing with the ">>>" prefix?
  • Well, you would get no output if the return value from a() is assigned to some variable or used in any other way ...
  • I found this as well, but I would take "normal output" to mean output, not the results of expressions.
  • It's awkwardly worded, sure - I think I'd interpret returning as an output to the calling function, which here is the Python shell, but it's definitely ambiguous