Why does the last line in a cell generate output but preceding lines do not?
Question:
Given this Jupyter notebook cell:
x = [1,2,3,4,5]
y = {1,2,3,4,5}
x
y
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:
Answers:
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:
https://ipython.readthedocs.io/en/stable/config/options/terminal.html
One option is to add explicit print()
calls. So for example you can do
print(x)
print(y)
to print out both values.
Given this Jupyter notebook cell:
x = [1,2,3,4,5]
y = {1,2,3,4,5}
x
y
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:
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:
https://ipython.readthedocs.io/en/stable/config/options/terminal.html
One option is to add explicit print()
calls. So for example you can do
print(x)
print(y)
to print out both values.