Part 1: Google Colab

Kerry Back, Rice University

Open Colab

Open https://colab.research.google.com.

Select New Notebook.

What are Notebooks?

  • The Colab interface is a Jupyter notebook.
  • A notebook is a collection of cells.
  • Cells contain either explanatory text or executable code and its output.
  • You can add new cells by using the + CODE and + TEXT buttons in the toolbar or the buttons that show when you hover between cells.

Running Code: Assignment Statements

Type the following in a cell. Then hit the Play/Run button at the left side of the cell to execute it.

x = 10
print(x)
10

The statement x=10 assigns the value 10 to the variable x. The statement print(x) prints the value of x to the screen.

Practice: Variables

Exercise 1 (with Gemini): Ask Gemini to “write Python code that creates a variable named age with value 25 and prints it”

Exercise 2 (on your own): Type y = 100 then print(y) and run it.

Running More Code

Add a new code cell and type:

y = 2 * x
print(y)
20
  • Spaces around the = and * are optional.
  • Now we have two variables in our Python session. They are independent.
  • If we change x, it will not affect y.
x = 0
print(y)
20

Practice: Operations

Exercise 1 (with Gemini): Ask Gemini to “write Python code that creates a variable z equal to 5 times 3 and prints it”

Exercise 2 (on your own): Type a = 10 + 20 then print(a) and run it.

Saving and Opening Notebooks

  • Notebooks are portable. They can be saved and used in many other environments.
  • Save your notebook to your Google drive and then close it.
  • Open it again from Google drive.
  • Open a code cell, type print(x) and run the cell.
  • You will get an error message that x is not defined.

Notebooks vs Spreadsheets

  • Notebooks are similar to spreadsheets in some ways but differ in others.
  • Assigning the value 10 to x and defining y as 2*x is similar to having the value 10 in cell A1 in a spreadsheet and putting =2*A1 in cell B1.
  • The cell names in a spreadsheet are similar to variable names in Python.

Key Difference: Execution Order

  • An important difference: all formulas in a spreadsheet become active when we open it, but nothing is activated when we open a notebook.
  • When we open a notebook, we are just getting an image of a text file rendered by our browser.
  • Python has not yet done anything. It doesn’t know any variables and it doesn’t know any formulas.

Execution Order Matters

  • Python becomes active when we execute a code cell. Only the executed code cells are meaningful.
  • Code cells are meaningful in the order they are executed, not in the order in which they appear in the notebook.
  • This is important to keep in mind when moving around in a notebook.

Tip: If it becomes confusing, use Runtime/Restart Session to clear Python, then use Runtime/Run Before to run all cells before a selected cell.

Text Cells

This is a text cell. You can double-click to edit this cell. Text cells use markdown syntax.

To learn more, see the markdown cheat sheet.

Gemini: Within-Cell Code Generation

Create a new code cell. If you choose generate with AI, a prompt window will appear within the cell.

Tell Gemini what you want, and it will write code for it.

Example: Tell Gemini you want to calculate the sum of the first 100 integers. Click the play button after Gemini writes the code, and the cell will execute.

Gemini: Chatbot

If you want advice instead of code or in addition to code, click the Gemini at the top right of the Colab window to open a chatbot.

If you ask the chatbot to do something, it will add code cells to the notebook and generate code for you.

Error Messages

Everyone makes errors. We need to learn to read error messages to find the line of code with the error and to get information about the error.

x = 1
y = 4
z = y/(1-x)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[4], line 3
      1 x = 1
      2 y = 4
----> 3 z = y/(1-x)

ZeroDivisionError: division by zero

If an error message isn’t clear, ask the chatbot to explain it and how to fix the error.

Practice: Error Messages

Exercise 1 (with Gemini): Ask Gemini to “explain what ZeroDivisionError means and how to avoid it”

Exercise 2 (on your own): Type print(10 / 2) and run it (this works correctly).