Goals of this page #
- Understand the basics of Google Colab and run your first Notebook
- Learn the advantages and caveats of working in Colab
- Collect tips for migrating to a local environment later
Why choose Colab? #
| Pros | Points to note |
|---|---|
| No installation required—you can start right away | Requires an Internet connection |
| GPU/TPU resources available in the free tier (for limited time) | Runtimes reset after being idle for a while |
| Easy to share notebooks | Files disappear when the session ends (mount Drive to persist) |
If you are unsure about setting up locally, get comfortable with Python in Colab first, then step up to a uv-based environment later.
What you need #
- Google account
Create one at https://accounts.google.com/ if you do not already have it. - Browser
Google Chrome (latest version) is recommended. Other Chromium-based browsers generally work too.
Create and run a Notebook #
- Go to Google Colab and sign in with your Google account
- Click “New notebook”
- In the first cell, enter the following code and press
Shift+Enter
print("Hello, Python from Colab!")
If you see a check mark and execution time on the left of the cell, it worked.
Working with files #
- Temporary files
- Use the cell magic
%%writefile sample.pyto create a short-lived file. - Remember: when the runtime resets, temporary files are removed. Mount Drive if you need persistence.
- Use the cell magic
- Mounting Google Drive
from google.colab import drive drive.mount('/content/drive')- Follow the authorisation flow and agree; your Drive will appear under
/content/drive/MyDrive.
- Follow the authorisation flow and agree; your Drive will appear under
- Upload / download files
from google.colab import files files.upload() # opens a file chooser dialog files.download("result.csv")
Runtime types #
- Menu “Runtime” → “Change runtime type” lets you switch to GPU or TPU
- The free tier has time limits, so it is not suitable for very long jobs
- Features that keep the session alive automatically can violate the terms—check the usage policy
Installing common libraries #
Colab comes with many libraries pre-installed, but you can pin versions with pip.
!pip install pandas==2.2.1
!pip install "scikit-learn>=1.4,<1.5"
Prefix ! to execute shell commands. Note that you need to reinstall packages each time the runtime restarts.
Organising notebooks #
- Notebooks are saved to Google Drive (default:
My Drive/Colab Notebooks) - Use “File” → “Save a copy in Drive” to clean up folders and keep things tidy
- If you need versioning/history, connect the notebook to GitHub
Limitations and workarounds #
| Limitation | Workaround |
|---|---|
| Session disconnects | Save important files to Drive / keep short execution logs |
| Storing external API keys | Prefer google.colab.auth together with a secrets manager over environment variables |
| GUI apps cannot run directly | For tools like Streamlit, expose via localtunnel or ngrok |
Tips for moving to a local environment #
- Download notebooks (
.ipynb) and open them in VS Code to keep working locally - Export package versions to
requirements.txtso you can recreate the setup later - Once dependencies grow, manage them with commands such as
uv pip freeze > requirements.txt
For learning syntax and control flow, Colab is more than enough. When you are ready to try command-line work or virtual environments, continue with the local setup guides:
When you’re ready, move on to the next lesson to start learning Python.