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!"]
Google Colab
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.
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
namepoints to the textAda. - The first
printdisplays the current value, so it displaysAda. - The second assignment changes the value attached to
name. - The second
printdisplaysGracebecause 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
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
- Copy Example 1.1 into a new Colab cell.
- Change exactly one value, name, condition, or line.
- Write the expected output before running the cell.
- Run the cell and compare the actual result with your prediction.
- 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
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.
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:
Step-by-step explanation
name = "Ada"- Python creates the string
"Ada". - The name
namenow refers to that string.
- Python creates the string
print(f"Welcome, {name}!")- The
fcreates an f-string. {name}is replaced with the value"Ada".- The output is
Welcome, Ada!.
- The
- 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:
Cell 2:
If you run Cell 2 before Cell 1, Python raises NameError because name does not exist yet. Run Cell 1 first, then Cell 2.
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:
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:
- Prediction cell: write what you expect in a Markdown cell.
- Original code cell: run the lesson example unchanged.
- Experiment cell: copy the code and change one thing.
Example experiment:
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
- 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/