
Python in Google Colab
Google Colab (short for Colaboratory) is a free, browser-based platform from Google that lets you write and run Python code — no setup required! Perfect for learning, data science, and machine learning.
Why Use Google Colab?
- Runs in your browser — no install needed
- Free access to GPUs/TPUs (great for AI/ML)
- Saves to Google Drive automatically
- Built-in support for charts, tables, and markdown
- Easy to share with others
Open Google Colab
Go to https://colab.research.google.com
Click: > New Notebook → Creates a new .ipynb (Jupyter Notebook)
Write & Run Python Code
You’ll see a cell with a play button ▶️.
Type Python code inside it:
print("Hello, Colab!")Click the ▶️ button (or press Shift + Enter) to run it.
Output appears right below the cell.
Add More Cells
- Click + Code to add a new code cell.
- Click + Text to add a Markdown/text cell (for notes, headings, etc.).
Example:
# Basic math
x = 5
y = 10
print(f"{x} + {y} = {x + y}")Upload & Use Data Files
Want to load a CSV or image?
from google.colab import files
uploaded = files.upload() # Opens file pickerThen read it (e.g., with pandas):
# Uncomment and run after uploading a CSV file
# import pandas as pd
df = pd.read_csv("yourfile.csv")
print(df.head())Pro Tips
Use
!to run shell commands:!pip install numpyEnable GPU:
Runtime → Change runtime type → Hardware accelerator → GPUUse keyboard shortcuts:
Ctrl + M H→ Show all shortcuts
Summary
| Task | How to Do It in Colab |
|---|---|
| Run Python code | Type in cell → Shift + Enter |
| Add new cell | Click + Code |
| Upload files | files.upload() |
| Install packages | !pip install package_name |
| Save notebook | Auto-saves to Google Drive |
| Share notebook | Click Share button |
