Usage tips#

This document/website that you’re reading right now is created using Jupyter Book. Here, we will take a moment to explain the UI to help you get the most out of the experience.

Interactivity#

Several of the pages of this book are actually Jupyter notebooks, which provide interactive programming environments that interleave text, code, and graphics to provide hands-on learning experiences for students. On these pages, you will be able to run Python code in real time, both for the tutorials to learn the commands and for your actual homework exercises.

Note

If you’ve ever used MATLAB Live Scripts, this is basically the Python equivalent (not sure which came first).

Why Python? 🐍#

There are many programming languages one can choose from, and we eventually settled on Python as it has skyrocketed in popularity in recent years. This growth is in part due to:

  1. Its readability. Python is designed to be simple and reads like English.

  2. Its extensibility. Python makes it easy for developers to write modules that extend its functionalities for applications in data science, scientific computing, and flying drones on Mars.

  3. Its open-source properties. This means anyone can contribute to Python development and anyone can use it—for free!

Launching Colab#

To launch any notebook in Google Colab, hover over the icon in the top-right corner, then Ctrl+click the button ∞ Colab (open in new tab), and wait for the environment to load. Note that not all pages are interactive, and thus not all pages will have the rocket symbol.

Tip

All of the top-level pages for each chapter, like Saving your work, are interactive, so if you want to run and modify the example code yourself, you can!

Attention

Try it now by clicking on the for this page and read the rest of the page in Colab!

Running a Jupyter notebook#

The information in Jupyter notebooks is organized into cells, which come in many forms.

Markdown cells#

This cell is called a Markdown cell, which is used for text/media that can be formatted with the Markdown markup language. You know this is a Markdown cell because there is a white background and no ▶️ on the left side when you’ve selected the cell. If you double-click a Markdown cell, you can edit it; press Shift+Enter to save your edits and exit the cell.

Code cells#

Notice how the next cell looks a little different. It is a code cell, which allows you to write Python code and then execute the code with Shift+Enter. Or you can click the ▶ Run button on the left side to execute the code.

Quick exercise: In the space below, enter your name between the quotation marks and then run the code cell by pressing Shift+Enter.

# inline comments in Python start with "#"
name = ""          # enter your name here as a string, enclosed by quotes
print(f'Hello, {name}!')  

You should see some output and a number appear between the square brackets! (e.g., In [1]:) This number indicates the sequence of code cell execution, which can be handy for a few reasons:

  • You can clearly tell which cells have been executed and which cells have not.

  • You can split code among several code blocks and run them in whatever order you want.

  • You can easily change an earlier code cell and rerun it.

Important

We hope you caught that! Use Shift+Enter to execute the cells! Either to run code or save any Markdown text that you’ve edited.

Saving your work#

Unfortunately, if you close Colab (or even disconnect from the runtime environment), your work won’t be saved. To save your own copy of this page, you should make a copy of this notebook by going to File > Save a copy in Drive. You’ll find it in the folder My Drive > Colab Notebooks, and you’re welcome to move it elsewhere. This allows you to save and return to the notebook with your edits preserved.

Uploading and downloading data#

After you’ve copied the notebook, you can upload any files to your Colab workspace by dragging it into the empty space in the Files tab (📁) on the left side (YouTube tutorial). It will warn you that the file will disappear if the runtime disconnects. That’s OK, you can always upload it again.

If your code generates any files (like a saved PNG image), it will also appear in the sidebar where you can download it to your computer.

Getting help#

We would be remiss not to mention that, in addition to your instructors and peers, generative AI is a great tool to help with generating code, learning its structure/syntax, and debugging! You can ask questions about parts that are unclear or directly copy-paste any error messages to help you resolve it. Try it for free on the Stanford AI Playground!

Vibe coding and the illusion of explanatory depth#

That being said, you should not be overly dependent on the code that AI produces, at least not to the point where you cannot interpret it and can only hope it is 100% correct. It is true that AI will probably get all of the exercises correct and in the future a lot of the code we write will be AI produced. But in order to identify errors/gaps and fine tune things—heck, even just being able to read code—requires some fundamental skills on your part, and that’s what we hope these exercise will help you foster. We encourage you to double check your understanding of every line of code, just so you’re not fooling yourself!

Exporting your work#

When you’re ready, the easiest way to export the notebook is to File > Print it and save it as a PDF. Remove any excessively long, unrelated outputs first by clicking the arrow → next to the output box and then Show/hide output. Obviously don’t obscure any necessary output or graphs!

Conclusion#

This completes our introduction to the Jupyter Book interface. You’re ready to start with Exercise 1!