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 user interface 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 the Python equivalent (and predecessor).

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 Random number generation, 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 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. It will warn you anytime you run a notebook that wasn’t created by you, just to be safe!

# 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., [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 long term#

Unfortunately, if you close Colab, 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#

You can upload files to your Colab workspace by dragging them into the empty space in the Files tab (📁) on the left side (YouTube tutorial). This will allow you to read external data files such as .csv and .txt when solving problems. 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 this 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 them. In fact, Colab has Google Gemini integrated to assist with code completion, and you can also try other models for free on the Stanford AI Playground. We recommend you acknowledge any use of generative AI in accordance with the Stanford Honor Code.

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 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-generated. 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 develop. 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 to submit, the easiest way to export any 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!