Tuesday Tip #29: Reuse Python code with magic commands ๐ŸŽฉ๐Ÿ‡


Hi Reader,

Wherever you are in the world today, I wish you safety, health, and happiness! ๐Ÿ’—


๐Ÿ”— Link of the week

โ€‹Unofficial Python glossaryโ€‹

This guide was written by my pal Trey Hunner, and itโ€™s the single best source Iโ€™ve found for clear explanations of Python terms and concepts. I use it to learn new things and to double-check that my teaching materials are correct!


๐Ÿ‘‰ Tip #29: Faster coding using magic commands

Back in tip 24, I introduced you to IPython magic commands, which are special commands that you can use in Jupyter.

You learned about line magics, which start with % and apply to one line of code:

  • %lsmagic
  • %quickref
  • %time
  • %timeit
  • %who
  • %whos
  • %history
  • %pastebin

You also learned about cell magics, which start with %% and apply to an entire cell:

  • %%time
  • %%timeit

Today, Iโ€™m going to introduce you to 4 more magic commands that are great for saving, displaying, and running code!


Save & reuse Python code without leaving Jupyter

Letโ€™s pretend that youโ€™re working in a large Jupyter notebook and you come up with a brilliant new function:

You want to save this function to reuse in other notebooks. Without leaving Jupyter, you use the %%writefile cell magic to save just this function to another Python script:

And now your function is preserved in a separate file!

Weeks pass, and youโ€™re working in a new notebook that could benefit from this function. You want to remind yourself whatโ€™s in the function, so you output the file contents using the %pycat line magic:

If it needed some edits, you could use the %load line magic:

Running that command loads the contents of the file into the cell (but does not run it) so that you can make those edits:

But in this case, the function is perfect as-is. Thus you use the %run line magic to run the existing file:

Our function is now available for use in this new notebook:

Key takeaway: With this workflow, you can save and reuse code blocks without ever leaving the Jupyter environment!


You can also %run an entire notebook

By the way, %run even works with notebooks! For example, if you had this notebook called drinks.ipynb:

You could run the entire notebook from another notebook, and just see its output:

Key takeaway: You can build a series of smaller notebooks, and then run them all from a โ€œmasterโ€ notebook that only displays the output (and hides the underlying code).


If you enjoyed this weekโ€™s tip, please forward it to a friend! Takes only a few seconds, and it really helps me reach more people! ๐Ÿ™

See you next Tuesday!

- Kevin

P.S. Which came first, the chicken or the egg?โ€‹

Did someone awesome forward you this email? Sign up here to receive Data Science tips every week!

Learn Artificial Intelligence from Data School ๐Ÿค–

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School ๐Ÿค–

Hi Reader, On Friday, I announced my forthcoming book, Master Machine Learning with scikit-learn. In response, my Dad asked me: How does the subject of this book relate to Artificial Intelligence? In other words: What's the difference between AI and Machine Learning? Ponder that question for a minute, then keep reading to find out how I answered my Dad... ๐Ÿ‘‡ AI vs Machine Learning Here's what I told my Dad: You can think of AI as a field dedicated to creating intelligent systems, and Machine...

Hi Reader, Yesterday, I posted this announcement on LinkedIn and Bluesky and X: Kevin Markham @justmarkham Dream unlocked: I'm publishing my first book! ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰ It's called "Master Machine Learning with scikit-learn: A Practical Guide to Building Better Models with Python" Download the first 3 chapters right now: ๐Ÿ‘‰ https://dataschool.kit.com/mlbook ๐Ÿ‘ˆ Thanks for your support ๐Ÿ™ 1:47 PM โ€ข Sep 11, 2025 1 Retweets 5 Likes Read 1 replies This has been a dream of mine for many years, and I'm so excited...

Hi Reader, Hope youโ€™ve had a nice summer! โ˜€๏ธ As for me, Iโ€™ve been finishing my first ever book! I canโ€™t wait to tell you about it and invite you to be part of the launchโ€ฆ stay tuned ๐Ÿ‘€ Today's email focuses on a single important topic: AIโ€™s impact on your mental health ๐Ÿง  Read more below! ๐Ÿ‘‡ Sponsored by: Morning Brew The 5-Minute Newsletter That Makes Business Make Sense Business news doesn't have to be dry. Morning Brew gives you the biggest stories in business, tech, and finance with quick...