Setting Up Google Colab

This curriculum uses Python, a general-purpose programming language widely used in science and engineering. Python has an extensive collection of libraries that makes it especially useful for scientific computing and applied mathematics.

Python Notebooks

There are several ways to write programs in Python. In this course, we will use Google Colaboratory notebooks (Google Colab) to write code and create files to submit for grading. Colab allows Python code to be written and run in a web browser without requiring any software installation. Notebooks also allow Python code to be executed in small blocks, which makes it easy to run, test, and fix code quickly.

Do I have to use Google Colab?

If you have previous programming experience, you are welcome to complete the labs in an editor other than Google Colab as long as you disable any AI tools for writing code (see below).

Disable Google AI Tools

AI tools that generate code for you are off limits in this course; see About This Course > Avoid Generative AI. Before you do anything else, disable the AI tools in Google Colab. Click the Settings gear in the top right corner, and under AI Assistance,

  • clear Show AI-powered inline completions,

  • clear Consented to use generative AI features, and

  • select Hide generative AI features.

Other Colab Settings

We recommend (but do not require) the following Google Colab settings. Click the Settings gear in the top right corner, and under Editor,

  • set Indentation width in spaces to 4 instead of 2,

  • select Show line numbers,

  • select Show indentation guides,

  • select Enable code folding in the editor, and

  • select Automatically close brackets and quotes in code cells.

Copying Template Files

A Google Colab notebook is provided as a template at the start of each lab. This file should be copied to your own Google Drive before you begin coding.

  1. On the lab outline page, click the button labeled “Open Lab Notebook in Google Colab.”

  2. From the Colab menu, select File > Save a Copy in Drive.

  3. If prompted, sign in to a Google account. Colab will open a new copy of the notebook and save it to Google Drive.

Template notebooks generally contain instructions and incomplete functions to be implemented.

Executing Code in a Colab Notebook

A notebook is organized into cells, blocks that contain either Python code or formatted text. To add a new cell, use the + Code or + Text buttons near the top of the notebook.

Code Cells

A code cell contains Python code. To run a code cell, click the play button on the left side of the cell, or press Shift+Enter while the cursor is in the cell. Colab executes the code in the cell and displays any outputs directly below it. If the last line of a code cell is an expression, Colab displays the value of that expression.

2 + 3
5

Use print() to display values from any line in a code cell. Without print(), Colab displays only the value of the final expression.

1 - 1  # This result will not be displayed.
print(1 + 1)
print(2 + 3)
10 + 4
2
5
14

If a code cell contains an error, Colab displays an error message below the cell instead.

7 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[3], line 1
----> 1 7 / 0

ZeroDivisionError: division by zero

Text Cells

A text cell contains formatted non-code notes. Text cells are written in Markdown, a simple formatting language for headings, lists, links, and other text. Double-click a text cell to edit it; use Shift+Enter to render the text.

LaTeX

Text cells can display mathematical notation written in LaTeX. Use single dollar signs for math within a sentence, such as $x^2 + y^2 = z^2$ to produce \(x^2 + y^2 = z^2\). Use double dollar signs for displayed equations, for example,

$$
    \int_0^1 x^2\, dx = \frac{1}{3}.
$$

which renders as

\[ \int_0^1 x^2\, dx = \frac{1}{3}. \]

Runtimes

Colab runs code using a Python process called the runtime or kernel. The kernel remembers variables and imported packages from cells that have already run. For example, if x = 14 is executed in one cell, later cells can use x as long as the same runtime is still active.

The Runtime menu controls this kernel. Other notebook programs sometimes call the same controls the Kernel menu.

  • Use Runtime > Restart session to clear the kernel’s memory and start fresh.

  • Use Runtime > Run all to execute every code cell from top to bottom.

  • Use Runtime > Restart session and run all to do both actions.

Order of Execution

Notebook cells can be manually executed in any order. However, this can lead to problems later on. Consider the following cells.

print(x)
x = 10

Running the second cell before the first works because x is defined before it is printed. After restarting the runtime, running the first cell first raises a NameError because x has not yet been defined.

Cells are intended to be run from top to bottom, and this is how the grader will read submitted files. It is good practice to restart the session often and fix resulting errors as you go.

Lab Submission

After completing a lab, stop and check for errors.

  1. In the Runtime menu at the top of Google Colab, select Restart session and run all.

  2. If any of the cells produce error messages, fix the error(s).

  3. Repeat steps 1 and 2 until there are no error messages. Your code cannot be graded if there are any error messages.

Next, in the File menu select Download > Download .py. The downloaded file can be uploaded to gradescope.com for grading.