Google Colab

getting-started
beginner
Run Python in a browser and learn the notebook workflow without installing software.
  • Level: Beginner
  • Estimated time: 20–30 minutes
  • You will learn: Run Python in a browser and learn the notebook workflow without installing software.
  • Practice in: Google Colab and browser notebooks

Questions

  • What problem does Google Colab help us solve in a small Python program?
  • What should we predict before running the example?
  • What value, output, or error should we inspect after changing one line?

Objectives

  • Run a complete example for Google Colab as a practice notebook in Colab.
  • Explain the example line by line using plain language.
  • Change one part of the code and predict the result before running it.
  • Recognize one common mistake and use the error message as evidence.

Hands-on episode: Google Colab

Colab notebooks are made of cells. Code cells run Python; Markdown cells hold explanations, predictions, and reflections. The order you run cells matters because Python remembers values in the current runtime.

We will learn this by running code, not by memorizing a definition first. Open the Colab notebook from the button above, find this section, and run each cell in order. Keep a small note beside the notebook with three columns: prediction, actual result, and what changed.

Example 1.1

Use two cells to see the difference between the notebook order on the page and the order Python actually ran.

name = "Ada"
print(name)

name = "Grace"
print(name)

Run the cell once without editing it. If the result is different from your prediction, leave the prediction visible and write one sentence about the difference. That sentence is more useful than a perfect first guess.

Explain Example 1.1

  • When the first assignment runs, the name name points to the text Ada.
  • The first print displays the current value, so it displays Ada.
  • The second assignment changes the value attached to name.
  • The second print displays Grace because that is the most recent value in the runtime.

Now explain the example out loud or in a Markdown cell. Use short sentences: “this line creates…”, “this name stores…”, “this output appears because…”. If you cannot explain a line yet, run only the lines above it and inspect the values that exist at that moment.

Challenge 1.1

NoteChallenge

Run only the last print(name) in a fresh runtime. You should get a NameError because the runtime has forgotten previous work. Then run the assignment cell first and try again.

Show a safe way to approach the challenge
  1. Copy Example 1.1 into a new Colab cell.
  2. Change exactly one value, name, condition, or line.
  3. Write the expected output before running the cell.
  4. Run the cell and compare the actual result with your prediction.
  5. If the result surprises you, undo the change and try a smaller one.

Suggested first move: Run only the last print(name) in a fresh runtime.

Debugging checkpoint 1.1

WarningDebugging checkpoint

A common notebook mistake is hidden state: a cell works because you ran another cell earlier, but the page does not make that dependency obvious. If output feels impossible, choose Runtime > Restart runtime, then run cells from top to bottom. That turns the notebook back into an honest story.

Do not debug by rewriting the whole example. Read the error type or surprising output, inspect the closest value with print(...) or type(...), then change one thing. This is the same routine you will use in larger projects.

Apply it

Create three cells: a Markdown prediction, a code cell with two assignments and prints, and a Markdown reflection. Save a copy of the notebook, restart the runtime, and confirm that the notebook still works when run from the top.

Finish by adding a Markdown cell that answers: What did this example teach me that I can reuse in a project?

Key points

  • Learn the concept by running a complete, small example first.
  • Predict before execution so your thinking becomes visible.
  • Change one thing at a time so cause and effect stay clear.
  • Treat errors as clues about the exact line or value Python could not handle.

The problem this lesson solves

You can learn Python before installing anything on your computer. Google Colab runs notebooks in the browser, which makes it a friendly place to practice code, write notes, see output, and share work.

Colab is especially useful at the beginning because it removes setup problems. You can focus on code first, then later learn local tools such as VS Code and Jupyter.

What a notebook is

A notebook is a document made of cells. A cell can contain code or notes. When you run a code cell, Python executes that cell and shows the output below it.

How notebook cells run

A notebook keeps code, output, and explanation close together.

flowchart TD
  notebook["Notebook"] --> markdown["Text/Markdown cell<br/>explain the idea"]
  notebook --> code["Code cell<br/>name = 'Ada'"]
  code --> runtime["Runtime<br/>Python process"]
  runtime --> output["Output under the cell<br/>Welcome, Ada!"]

The runtime remembers values from cells that have already run. If results become confusing, restart the runtime and run cells from top to bottom.

Vocabulary in plain language

Word Beginner-friendly meaning
Notebook A document containing notes, code cells, and output.
Cell One editable block in a notebook.
Code cell A cell that Python can run.
Markdown cell A cell for formatted notes, headings, and explanations.
Runtime The Python session connected to the notebook.
Output What appears below a code cell after it runs.

First notebook exercise

Open https://colab.new. Create or use the first code cell and run:

name = "Ada"
print(f"Welcome, {name}!")

Step-by-step explanation

  1. name = "Ada"
    • Python creates the string "Ada".
    • The name name now refers to that string.
  2. print(f"Welcome, {name}!")
    • The f creates an f-string.
    • {name} is replaced with the value "Ada".
    • The output is Welcome, Ada!.
  3. The output appears below the code cell.

Cell order matters

A notebook can be run out of order. That is powerful, but it can also confuse beginners.

Try this in two separate code cells.

Cell 1:

name = "Ada"

Cell 2:

print(name)

If you run Cell 2 before Cell 1, Python raises NameError because name does not exist yet. Run Cell 1 first, then Cell 2.

WarningCommon mistake

If notebook output stops making sense, restart the runtime and run all cells from the top. This clears old hidden state.

Notes and code together

Add a Markdown cell above your code and write:

## Greeting experiment

Prediction: the code will print a welcome message for Ada.

Good notebooks explain the purpose of the code, not just the final answer. Use notes for predictions, observations, and questions.

Check your understanding

Practice routine in Colab

Use this three-cell pattern for course exercises:

  1. Prediction cell: write what you expect in a Markdown cell.
  2. Original code cell: run the lesson example unchanged.
  3. Experiment cell: copy the code and change one thing.

Example experiment:

name = "Grace"
print(f"Welcome, {name}!")

Common Colab problems and fixes

Problem What to try first
NameError for a value defined earlier Run the earlier cell, or restart and run from top.
Output looks old Run the current cell again.
Too much output Stop or interrupt the running cell.
Notebook is not saved Save a copy in your own Drive or download a copy.

Check your understanding again

Key points

TipKey points
  • Colab lets you run Python in a browser.
  • A notebook contains code cells, note cells, and output.
  • The runtime remembers values from cells that have run.
  • Running cells out of order can cause confusing results.
  • Restarting and running from top is a reliable notebook debugging habit.

References

  • Google Colab: https://colab.new
  • Colab overview: https://colab.research.google.com/
  • Markdown guide: https://www.markdownguide.org/basic-syntax/
Back to top