πŸ§™β€β™‚οΈ Jupyter Notebook Magic Commands – The Ultimate Cheat Sheet for Productive Python

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

Python
%lsmagic

Lists all available magic commands on your system.


πŸ“œ 2. Quick Help and Info

Python
%quickref        # IPython quick reference card
%pinfo variable  # Info about a variable (also: variable?)
%pinfo2 variable # More detailed info (also: variable??)

⏱ 3. Timing Execution

Python
%%time            # Measures wall time of cell execution
%%timeit          # Runs code multiple times and reports average execution time

🧠 4. Variable Management

Python
%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

Python
%%writefile my_script.py
def hello():
    print("Hello, world!")

Creates and writes to my_script.py.

Python
%load my_script.py

Loads file content into a cell.

Python
%run my_script.py

Runs the Python script inside the notebook.


πŸͺ› 6. Debugging Tools

Python
%debug

Launches interactive debugger if an error occurred in the previous cell.


πŸ’Ύ 7. History and Recall

Python
%history -n      # Show previous commands with line numbers
%recall 5        # Recall command at line number 5

βš™οΈ 8. Environment & Configuration

Python
%env              # Shows environment variables
%precision 3      # Set float precision

πŸ§ͺ 9. Magic for Shell & System Access

Python
%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

Python
%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

Python
%%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 ?command to get documentation for any magic command.
  • %matplotlib inline is 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

πŸ’Œ Stay Updated with PyUniverse

Want Python and AI explained simply straight to your inbox?

Join hundreds of curious learners who get:

  • βœ… Practical Python tips & mini tutorials
  • βœ… New blog posts before anyone else
  • βœ… Downloadable cheat sheets & quick guides
  • βœ… Behind-the-scenes updates from PyUniverse

No spam. No noise. Just useful stuff that helps you grow one email at a time.

πŸ›‘οΈ I respect your privacy. You can unsubscribe anytime.

Leave a Comment