Whether youβre just starting out in Python or you’re an experienced data scientist, Jupyter Notebook is one of the most powerful tools in your workflow. And if you want to make the most of it, you need to understand Magic Commands special shortcuts that extend Python’s capabilities right inside your notebooks.
In this post, weβll explore a comprehensive cheat sheet of Jupyter Notebook Magic Commands, explain how and when to use them, and provide practical examples. Plus, weβll go beyond the standard list and add some advanced magic tips to level up your productivity.
πͺ What Are Jupyter Magic Commands?
Jupyter Notebook Magic Commands are special functions prefixed by % (line magic) or %% (cell magic) that offer enhanced functionality beyond standard Python syntax.
There are two types:
%line_magic: Applies to one line%%cell_magic: Applies to the entire cell
Letβs explore both standard and advanced commands below.
π Full Magic Commands Cheat Sheet (with Examples)
π 1. View Available Magics
%lsmagicLists all available magic commands on your system.
π 2. Quick Help and Info
%quickref # IPython quick reference card
%pinfo variable # Info about a variable (also: variable?)
%pinfo2 variable # More detailed info (also: variable??)β± 3. Timing Execution
%%time # Measures wall time of cell execution
%%timeit # Runs code multiple times and reports average execution timeπ§ 4. Variable Management
%who # List all defined variables
%whos # Detailed variable info
%store x # Save variable x for use in later notebooks
%store -r # Reload all stored variablesπ 5. File Operations
%%writefile my_script.py
def hello():
print("Hello, world!")Creates and writes to my_script.py.
%load my_script.pyLoads file content into a cell.
%run my_script.pyRuns the Python script inside the notebook.
πͺ 6. Debugging Tools
%debugLaunches interactive debugger if an error occurred in the previous cell.
πΎ 7. History and Recall
%history -n # Show previous commands with line numbers
%recall 5 # Recall command at line number 5βοΈ 8. Environment & Configuration
%env # Shows environment variables
%precision 3 # Set float precisionπ§ͺ 9. Magic for Shell & System Access
%pwd # Show current directory
%cd .. # Change working directory
%ls # List files in current directory
%mkdir new_folder # Make new folderπ 10. Advanced: Parallel Processing and Profiling
%prun my_function() # Profile a function's performance
%lprun -f func func() # Line-by-line profiler (if line_profiler installed)π§ͺ 11. Cell Magics for Language Integration
%%bash
echo "Hello from Bash!"
%%html
<h2>This is rendered as HTML</h2>
%%latex
\frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x - \mu)^2}{2\sigma^2}}π‘ Bonus Tips for Working with Magics
- Use
?commandto get documentation for any magic command. %matplotlib inlineis essential for plotting inside the notebook.- Combine magics in teaching notebooks to execute code, show HTML, and write to files in one flow.
β When Should You Use Magic Commands?
Magic commands help when you:
- Need to prototype code quickly
- Want to measure performance or memory usage
- Need to manipulate files and OS commands
- Are teaching or documenting workflows
They’re powerful, but use them responsibly too much magic can make your notebooks hard to debug or share.
π₯ Downloadable Version
π― Final Thought
Mastering Jupyter Notebook magic commands is like having shortcuts wired into your brain. The more you use them, the faster and smarter your notebooks become.
Bookmark this post. Share it with a friend. Or better yet make it your browser homepage until it becomes second nature.
Happy magicking! π§ββοΈ
Sufiyan Momin