Magic Commands in Python

Jahidul Hasan Hemal
1 min readApr 16, 2023

--

Art using Midjourney

Magic commands in Python are special commands that are available in IPython and Jupyter notebooks, and are not part of the standard Python language. These commands are prefixed with % or %%, and provide a range of helpful functionalities for interactive computing, data analysis, and debugging.

Here are some of the most commonly used magic commands in Python:

  • %run: Runs a Python script as if it were a program.
  • %load: Loads code from an external file into a code cell.
  • %time: Measures the execution time of a single statement.
  • %timeit: Measures the average execution time of a statement over multiple runs.
  • %who: Lists all the variables in the current namespace.
  • %whos: Provides more detailed information about all the variables in the current namespace.
  • %reset: Resets the namespace by removing all names defined by the user.
  • %debug: Activates the interactive debugger to help diagnose errors in code.
  • %matplotlib: Configures matplotlib to work with Jupyter notebooks.
  • %%writefile: Writes the contents of a code cell to an external file.

These are just a few examples of the many magic commands available in Python. You can see a full list of magic commands by running %lsmagic in an IPython or Jupyter notebook.

--

--

Jahidul Hasan Hemal
Jahidul Hasan Hemal

Written by Jahidul Hasan Hemal

A goddamn marvel of modern science. An open-source enthusiast and an optimist who loves to read and watch movies and is trying to learn how to write.

No responses yet