IPython Magic Commands in Jupyter Notebook - Part 1 | Step-by-Step Guide
Magic Commands¶
In Jupyter Notebook, Magic Commands are special built-in commands that provide additional functionality beyond normal Python commands.
They help users perform tasks such as file handling, timing code execution, managing variables, and controlling the notebook environment more easily.
Magic commands usually start with % for line magics and %% for cell magics.
They are designed to make working in Jupyter Notebook faster and more interactive.
1. %pwd¶
In Jupyter Notebook, %pwd is a magic command used to display the current working directory.
It stands for 'Print Working Directory'.
Shows the folder path where your notebook is currently running.
%pwd
'C:\\Users\\schol\\Projects\\Magic-Commands'
2. %ls¶
In Jupyter Notebook, %ls is a magic command used to list all files and folders in the current working directory.
It behaves like the ls command in Linux/macOS and dir command in Windows.
It helps you see what files are available in the notebook’s working folder.
%ls
Volume in drive C is Windows-SSD
Volume Serial Number is 565A-BBE5
Directory of C:\Users\schol\Projects\Magic-Commands
05/25/2026 16:12 <DIR> .
05/25/2026 16:01 <DIR> ..
05/25/2026 02:36 <DIR> .ipynb_checkpoints
05/25/2026 16:12 39,575 Magic-Commands-1.ipynb
05/24/2026 01:54 20,157 Magic-Commands-2.ipynb
05/25/2026 02:35 16,110 Magic-Commands-3.ipynb
05/25/2026 02:58 10,184 Magic-Commands-4.ipynb
05/25/2026 16:00 33 script.py
05/25/2026 16:04 <DIR> Test
5 File(s) 86,059 bytes
4 Dir(s) 143,095,631,872 bytes free
3. %cd¶
In Jupyter Notebook, %cd is a magic command used to change the current working directory.
It allows you to move from one folder to another inside the notebook environment.
It is useful when you want to work with files stored in different directories.
%cd Test
C:\Users\schol\Projects\Magic-Commands\Test
4. %who¶
In Jupyter Notebook, %who is a magic command used to list all variable names currently in the notebook.
It shows only the names of variables, not detailed information.
It helps you quickly see what variables are available in memory.
a=1
b=[1,2,3]
%who
a b
5. %whos¶
In Jupyter Notebook, %whos is a magic command used to display detailed information about all variables.
It shows variable names, types, and values.
It is more detailed than %who.
%whos
Variable Type Data/Info ---------------------------- a int 1 b list n=3
6. %history¶
In Jupyter Notebook, %history is a magic command used to show previously executed commands in the notebook.
It helps you track what code you have run.
It is useful for debugging and reviewing work.
%history
%pwd %ls %cd Test a=1 b=[1,2,3] %who %whos %history
7. %time¶
In Jupyter Notebook, %time is a magic command used to measure the execution time of a single statement.
It runs the code once and shows how long it takes.
It is useful for checking performance of small code snippets.
%time sum(range(100000))
CPU times: total: 15.6 ms Wall time: 12.7 ms
4999950000
8. %timeit¶
In Jupyter Notebook, %timeit is a magic command used to measure the average execution time of a code statement.
It runs the code multiple times for accurate timing.
It is more reliable than %time for performance comparison.
%timeit sum(range(100000))
6 ms ± 239 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
9. %matplotlib¶
In Jupyter Notebook, %matplotlib is a magic command used to enable plotting inside the notebook.
It allows graphs to be displayed directly below the code.
Most commonly used with inline option.
%matplotlib inline
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Sample Graph")
plt.show()
10. %run¶
In Jupyter Notebook, %run is a magic command used to execute an external Python script inside the notebook.
It helps you run .py files directly.
It is useful for testing or reusing code from other files.
%%writefile script.py
print("Hello from script file")
Overwriting script.py
%run script.py
Hello from script file
<Figure size 640x480 with 0 Axes>
Comments
Post a Comment