profile

Learn Data Science from Data School πŸ“Š

Tuesday Tip #29: Reuse Python code with magic commands πŸŽ©πŸ‡

Published 6 months agoΒ β€’Β 1 min read

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 Data Science from Data School πŸ“Š

Kevin Markham

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School πŸ“Š

Hi Reader, happy Tuesday! My recent tips have been rather lengthy, so I'm going to mix it up with some shorter tips (like today's). Let me know what you think! πŸ’¬ πŸ”— Link of the week A stealth attack came close to compromising the world's computers (The Economist) If you haven't heard about the recent "xz Utils backdoor", it's an absolutely fascinating/terrifying story! In short, a hacker (or team of hackers) spent years gaining the trust of an open-source project by making helpful...

13 days agoΒ β€’Β 1 min read

Hi Reader, Today's tip is drawn directly from my upcoming course, Master Machine Learning with scikit-learn. You can read the tip below or watch it as a video! If you're interested in receiving more free lessons from the course (which won't be included in Tuesday Tips), you can join the waitlist by clicking here: Yes, I want more free lessons! πŸ‘‰ Tip #43: Should you discretize continuous features for Machine Learning? Let's say that you're working on a supervised Machine Learning problem, and...

20 days agoΒ β€’Β 2 min read

Hi Reader, I'm so excited to share this week's tip with you! It has been in my head for months, but I finally put it in writing ✍️ It's longer than usual, so if you prefer, you can read it as a blog post instead: Jupyter & IPython terminology explained πŸ”— Link of the week Python Problem-Solving Bootcamp (April 1-21) Want to improve your Python skills quickly? There's no better way than solving problems, reviewing alternative solutions, and exchanging ideas with others. That's the idea behind...

about 1 month agoΒ β€’Β 3 min read
Share this post