Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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}
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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Same code as above!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading