Python for Data Science For Dummies
Book image
Explore Book Buy On Amazon
It’s kind of amazing to think that IPython provides you with magic, but that’s precisely what you get with the magic functions. A magic function begins with either a % or %% sign. Those with a % sign work within the environment, and those with a %% sign work at the cell level.

Note that the magic functions work best with Jupyter Notebook. People using alternatives, such as Google Colab, may find that some magic functions fail to provide the desired result.

The following list gives you a few of the most common magic functions and their purpose. To obtain a full list, type %quickref and press Enter in the IPython console or check out the full list.

Magic Function Type Alone Provides Status? Description
%%timeit No Calculates the best time performance for all the instructions in a cell, apart from the one placed on the same cell line as the cell magic (which could therefore be an initialization instruction).
%%writefile No Writes the contents of a cell to the specified file.
%alias Yes Assigns or displays an alias for a system command.
%autocall Yes Enables you to call functions without including the parentheses. The settings are Off, Smart (default), and Full. The Smart setting applies the parentheses only if you include an argument with the call.
%automagic Yes Enables you to call the line magic functions without including the % sign. The settings are False (default) and True.
%cd Yes Changes directory to a new storage location. You can also use this command to move through the directory history or to change directories to a bookmark.
%cls No Clears the screen.
%colors No Specifies the colors used to display text associated with prompts, the information system, and exception handlers. You can choose between NoColor (black and white), Linux (default), and LightBG.
%config Yes Enables you to configure IPython.
%dhist Yes Displays a list of directories visited during the current session.
%file No Outputs the name of the file that contains the source code for the object.
%hist Yes Displays a list of magic function commands issued during the current session.
%install_ext No Installs the specified extension.
%load No Loads application code from another source, such as an online example.
%load_ext No Loads a Python extension using its module name.
%lsmagic Yes Displays a list of the currently available magic functions.
%matplotlib Yes Sets the backend processor used for plots. Using the inline value displays the plot within the cell for an IPython Notebook file. The possible values are ‘gtk’, ‘gtk3’, ‘inline’, ‘nbagg’, ‘osx’, ‘qt’, ‘qt4’, ‘qt5’, ‘tk’, and ‘wx’.
%paste No Pastes the content of the clipboard into the IPython environment.
%pdef No Shows how to call the object (assuming that the object is callable).
%pdoc No Displays the docstring for an object.
%pinfo No Displays detailed information about the object (often more than provided by help alone).
%pinfo2 No Displays extra detailed information about the object (when available).
%reload_ext No Reloads a previously installed extension.
%source No Displays the source code for the object (assuming that the source is available).
%timeit No Calculates the best performance time for an instruction.
%unalias No Removes a previously created alias from the list.
%unload_ext No Unloads the specified extension.

About This Article

This article is from the book:

About the book authors:

John Paul Mueller is a tech editor and the author of over 100 books on topics from networking and home security to database management and heads-down programming. Follow John's blog at http://blog.johnmuellerbooks.com/. Luca Massaron is a data scientist who specializes in organizing and interpreting big data and transforming it into smart data. He is a Google Developer Expert (GDE) in machine learning.

This article can be found in the category: