Why does the last line in a cell generate output but preceding lines do not?


Given this Jupyter notebook cell:

x = [1,2,3,4,5]
y = {1,2,3,4,5}

When the cell executes, it generates this output:

{1, 2, 3, 4, 5}

The last line in the cell generates output, the line above it has no effect. This works for any data type, as far as I can tell.

Here’s a snip of the same code as above:

Same code as above!

Asked By: nicomp



You can change this behaviour with:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

The reason why only the last line is printed is that the default value of ast_node_interactivity is: last_expr.

You can read more about that here:

Answered By: Nexif

One option is to add explicit print() calls. So for example you can do


to print out both values.

Answered By: Code-Apprentice
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.