%magic commands for Jupyter Notebook Users

This article you would get well versed with different magic commands available in Jupyter Notebook with there usage in our day-to-day.

I have referred the documentation by IPython for my reference.

In order to see what all magic commands are provided by Ipython one could run %lsmagic command and get the list of magic commands.

You could easily remember this command, as ls here means list. Therefore it lists al the magic commands available.

Also, %magic will get you entire IPython documentation for magic commands.

%ls command : To list all files and folders in particular directory with date and time of last update/creation.

%cd command: Path of Current Directory

You could also change current directory to other using same command as done in command line.

For instance

%dhist command: Print your history of visited directories

As in above case we were somewhere in C Drive and then changed the current directory to D Drive.

%conda and %pip: Install library with current kernel

This is the most common use of magic commands.

%run command: To run a file from Juypter Notebook

File: hello_world.ipynb

On using %run command, we can run hello_world notebook and get the results.

%load command: To load code into the current frontend.

Using this command, you can load partial or entire code by specifying lines or ranges of lines to load from the source [-r <lines>], load a particular function or classes from python source [-s <symbols>] and many other options are available.This would increase re-usability of code, and changes done in common functions/LoCs would be later reflected into other notebooks using it.

File: hello_world.py

On running the above command, will replace the contents of the cell with an external script as shown below.

If you need only hello_world function, then

Here, -s hello_world is used to load only hello_world function. In case of multiple function or class loading, separate them using comma. On running the above command, will replace the contents of the cell with hello_world function as shown below.

%timeit and %%timeit command: Time execution of a Python statement or expression

The benefit of %timeit is that for short commands it will automatically perform multiple runs in order to attain more robust results. For multi line statements, adding a second % sign will turn this into a cell magic that can handle multiple lines of input.

For instance, if you need to create a list of 1,00,000 numbers in power of 3. We can write for loop in following two different ways,

You can see that list comprehensions are faster than the equivalent for-loop construction in the above case.

Magic commands such as %time and %%time are also there, they provides very basic timing functionality and print wall clock time and CPU time as measures.

%something?: Help on Magic Functions

Using ? in front of any magic command, will have docstrings, and useful documentation over that specific magic command.

Thanks for reading!

--

--

Smriti Changulani
Python Data Wrangling by Smriti Changulani

Past — Software Engineer. Present — Data Analyst. Future — Data Scientist