✨ Jupyter Magic Commands
Magic commands are special commands that you can run in Jupyter notebooks to perform special tasks.
Mastering this concept will significantly boost your Python data science skills!
💻 Code Example:
# ── Run these in a Jupyter Notebook cell ───────────────────── # 1. %timeit — measure execution time of a statement %timeit [x**2 for x in range(10_000)] # vs. NumPy approach import numpy as np %timeit np.arange(10_000)**2 # 2. %%time — measure an entire cell %%time total = 0 for i in range(1_000_000): total += i print(f"Sum: {total}") # 3. %who / %whos — list variables in namespace x = 42 name = "pynfinity" %whos # Shows type + size + value for each variable # 4. %history — see recent command history %history -n 1-10 # 5. %run — run an external script # %run my_pynfinity_script.py # 6. %matplotlib inline — render plots inside notebook %matplotlib inline import matplotlib.pyplot as plt plt.plot([1, 4, 9, 16], marker="o", color="royalblue") plt.title("Quick Plot with Magic") plt.show() # 7. %%writefile — write cell content to a file %%writefile pynfinity_hello.py def greet(name): return f"Hello from Pynfinity, {name}!" print(greet("santoshtvk")) # 8. %load_ext — enable extensions (e.g., autoreload) %load_ext autoreload %autoreload 2 # Automatically reloads modified modules
Keep exploring and happy coding! 💻