Jupyter and Google Colab: Where ML Code Is Written
ML code is written differently
If you have written Python before, you probably worked like this. Write a whole .py file, run it, it goes from top to bottom, you see the output, done. If something is wrong, fix it and run the entire file again.
That is fine for a program that takes a second. Now think about ML work. You load a data file with a million rows, which takes 30 seconds. You look at the first few rows. You notice a strange column. You draw a chart to check it. You try removing some rows. You draw the chart again.
If every small change meant rerunning everything from the top, including that 30-second load, you would spend your whole day waiting. ML work is mostly this kind of try, look, change, try again. It needs a different kind of tool. That tool is the notebook.
What a notebook is
Remember your rough notebook from maths class? You tried a step, checked it, wrote a small note to yourself, and tried the next step, all on the same page. A coding notebook works the same way.
A notebook is a document split into boxes called cells. There are two kinds:
Key Points
- Code cells: hold a few lines of Python. You run each one on its own, whenever you want, and its result appears right underneath it.
- Text cells: hold notes, headings and explanations, like "this chart shows Sundays are quieter".
text cell
Exam marks, first look
marks = [78, 92, 65, 88]sum(marks) / len(marks)max(marks)The magic is that the notebook remembers everything between cells. Load your big data file once, in one cell. After that, every other cell can use it straight away. No waiting, no reloading. You only rerun the cell you changed.
Those small numbers in square brackets, [1], [2], [3], tell you the order in which cells were run. Keep an eye on them. They matter more than they look, as you will see shortly.
Jupyter and Colab: what is the difference?
Jupyter is the original notebook tool. It is free and runs on your own computer. The name comes from three languages it was built for: Julia, Python and R. Notebook files end in .ipynb, and you will see that extension everywhere in ML.
Google Colab is Jupyter notebooks running on Google's computers instead of yours. You open it in a browser, and that is it. For learning ML, it has some big advantages:
Key Points
- Nothing to install. No setup errors, no "it works on my friend's laptop but not mine".
- NumPy, pandas, Matplotlib and scikit-learn are already installed.
- It runs on Google's machines, so an old or slow laptop is fine. Even a phone works in a pinch.
- You get access to a GPU for free (with limits), which helps later with deep learning.
- Notebooks save to your Google Drive and can be shared with a link, like a Google Doc.
Tip:Colab is not a competitor to Jupyter. It is Jupyter, hosted by Google. Anything you learn in one works in the other. This course uses Colab because it needs zero setup.
Your first Colab notebook
Try this now. It takes two minutes.
Key Points
- Go to colab.research.google.com and sign in with any Google account.
- Click "New notebook". You will see one empty code cell.
- Type a line of Python into it, like print("hello").
- Press Shift + Enter. The cell runs, the output appears below it, and the cursor moves to a new cell.
- Use the "+ Code" and "+ Text" buttons at the top to add more cells.
The very first cell you run might take a few seconds. That is Colab starting up a computer for you on Google's side. After that, cells run quickly.
Cells remember things
Type these into three separate cells and run them one after another:
1marks = [78, 92, 65, 88]1sum(marks) / len(marks)80.75
1marks.append(100)
2len(marks)5
Two things to notice. First, cell 2 uses marks even though marks was created in cell 1. The notebook kept it in memory.
Second, cell 2 has no print(), yet it still showed 80.75. In a notebook, whatever the last line of a cell works out to is shown automatically. That is why ML code in notebooks often ends with just a variable name on its own line. It is a quick way to look at something.
The one trap everyone falls into
Cells run in the order you click them, not the order they appear on the page. That freedom is useful, but it causes the most common notebook bug.
Say you run cell 3 again, by accident or to check something. It adds another 100. Now go back and run cell 2:
1sum(marks) / len(marks)87.16666666666667
The average changed, but the code in cell 2 did not. marks now holds six marks, including two 100s, because cell 3 ran twice. Nothing on the screen tells you that. If you shared this notebook, the person opening it would get a different answer from you.
Tip:The fix is a habit. Whenever results look strange, and always before you trust or share a notebook, open the Runtime menu, restart, and run all cells from the top. If it still works, it is real. The [numbers] next to each cell also help: if they are not in order from top to bottom, cells were run out of order.
Three more things worth knowing
Installing a library. If you need a library Colab does not have, run a cell like this. The ! at the start means "run this as a terminal command, not as Python":
1!pip install some-library-nameFiles do not stay. Colab gives you a fresh computer each session. If you upload a data file and come back tomorrow, it will be gone. Keep your files in Google Drive, or load them from a web link. Chapter 3 shows both.
Sessions time out. If you leave Colab idle for a while, it disconnects and forgets everything in memory. Your code is still saved, so just run all cells again.
Notebooks are not for everything
Notebooks are brilliant for learning, exploring data and trying ideas. That is most of what this course does, so we use them throughout.
But when a model is ready to be used for real, like Meera's kitchen predictor sending a WhatsApp message every evening in lesson 1.5, the code usually moves into normal .py files. Those are easier to test, to run automatically and to keep tidy. Chapter 8 covers that step.
Prefer your own computer? You can install Jupyter locally, or open .ipynb files directly in VS Code. Everything in this course works the same way there. It just takes a bit of setup first.
Where people get confused
Key Points
- "Colab is a different language." It is not a language at all. It is a place to write and run normal Python.
- "The code in a cell always gives the same result." Not if other cells changed things since. The result depends on everything that ran before, in the order it ran.
- "My file disappeared from Colab!" Uploaded files only last for one session. Keep important files in Google Drive.
- "My code runs on my laptop." In Colab, it runs on Google's computers. Your laptop just shows you the page.
Quick check
Why are notebooks so popular for ML, compared with running a whole .py file each time?
Because you can run one small piece at a time and see its result straight away, while slow steps like loading data only happen once. ML work is mostly trying, looking and adjusting, and notebooks are built for that.
In a notebook, a list that should have 4 items has 6. The code that builds it looks correct. What most likely happened?
A cell that adds items to the list was run more than once, or cells were run out of order. Restart and run all cells from the top to get a clean result.
What does the ! do in !pip install pandas?
It tells the notebook to run that line as a terminal command instead of Python code. Here, that installs a library.
Recap
Key Points
- A notebook is made of cells: code cells you run one at a time, and text cells for notes.
- The notebook remembers variables between cells, so slow steps only run once.
- The last line of a code cell is shown automatically, no print() needed.
- Jupyter runs on your computer. Colab is Jupyter in the browser, on Google's computers, with nothing to install.
- Cells run in the order you click. When in doubt, restart and run all from the top.
Next up: NumPy. You already saw it run 20 times faster than a normal loop. Now let us see how it works and why almost every ML library is built on it.
Theia
Can you explain this concept?
Practise Jupyter and Google Colab: Where ML Code Is Written with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.