• 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:

  1. %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.

  2. %who_ls - gives a list of strings back and allows the information to be back in Python.

  3. %reset - deletes all variables and resets back to defaults. It is essential to command this carefully because it will delete all variables.

  4. %reset_selective - allows us to delete specific variable names using glob-like expressions to find it.

  5. %psearch - searches through variable names using a restriction to the specified expression, such as *name.

  6. %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.

Conclusion

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.

Previous Post

How to Calculate Sales Growth in Excel?

Next Post

Episode 2: Live Research from a Million Dollar Machine

About The auther

New Posts

Popular Post