Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-use result of last line Jupyter

In Jupyter notebook, is there any way to re-use the output the line above, within a cell?

Coming from Mathematica, I often find it useful to write things commands which work on the output of the last line using %, here's a stupid example:

Integrate[f[x],x]
Limit[%,x->1] - Limit[%,x->0] 

In general one can write %%% for 3rd-last output etc. https://reference.wolfram.com/language/ref/Out.html

@nostradamus reminds me that underscore _ is the output of the last cell, at least in Python. (Get last answer .) I didn't initially ask this, but would particularly like to be able to do this within a cell, so as to be able to execute multiple steps with one shift-enter.

I would also like to know if there's a way of doing either of these in Julia instead of Python.

like image 927
improbable Avatar asked Sep 12 '25 02:09

improbable


2 Answers

You can use "_" and works generally in python environment.

enter image description here

like image 162
ozw1z5rd Avatar answered Sep 13 '25 16:09

ozw1z5rd


In julia, ans stores the result of evaluating the last statement.

4*2
ans/2

You may also be interested in checking out the piping syntax

4*2 |>
sqrt
like image 30
Michael K. Borregaard Avatar answered Sep 13 '25 16:09

Michael K. Borregaard