- author: Python and Pandas with Reuven Lerner
Understanding Magic Commands in Jupyter and IPython
As a Python trainer, Reuben Lerner wants to share the benefits of using magic commands in Jupyter and IPython. These commands can be interpreted by Jupyter to do something to itself or be translated to something else that is then sent to the back-end Python process.
Differentiating Regular Commands and Magic Commands
When typing in commands and executing them in Jupyter, the commands are being sent back to Python somewhere in a back-end process. Once executed, the results are then displayed on the screen. However, magic commands are different and start with a percent sign (%). This sign is not allowed to be in a Python variable function or class name, allowing Jupyter to differentiate between regular commands and magic commands.
Using Magic Commands
Using different magic commands in Jupyter and IPython, we can utilize the following features:
%who- shows the variables that have been defined, but will not show all of the globals that Python set up. Importing libraries, such as
random, will show as defined.
%who_ls- gives a list of strings back and allows the information to be back in Python.
%reset- deletes all variables and resets back to defaults. It is essential to command this carefully because it will delete all variables.
%reset_selective- allows us to delete specific variable names using glob-like expressions to find it.
%psearch- searches through variable names using a restriction to the specified expression, such as
%history- displays the input history, allowing for reviewing previous input data.
Using these magic commands in IPython and Jupyter can help to utilize data science more efficiently by manipulating specific data sets and only using variables that we need.
In summary, using magic commands is an integral part of working with Jupyter and IPython. They allow the data scientist to manipulate data sets effectively, reducing the amount of manual coding and increasing productivity and efficiency. Reuben promises to return with many more magic commands to show their versatility and productivity benefits.