← Back to Module 0: Languages & Platforms

Jupyter Notebooks & Google Colab

~30 min Interactive Python

Jupyter Notebooks are interactive documents that combine code, output, and narrative text. They've become the standard for exploratory data analysis, teaching, and sharing research. Google Colab is a free cloud-based version you can use in your browser.

Python Scripts (.py) vs. Notebooks (.ipynb): What's the Difference?

Before diving in, let's understand the key difference between traditional Python scripts and notebooks. It's not just about file formats—it's about how you interact with your code.

The Core Insight

In a .py script, you run the entire file at once. In a notebook, you run individual cells and see results immediately. This makes notebooks ideal for exploration and learning.

See the Difference in Action

analysis.py Run File
import pandas as pd
import matplotlib.pyplot as plt

# Load data
df = pd.read_csv('sales.csv')

# Calculate average
avg = df['revenue'].mean()
print(f"Average: {avg}")

# Create plot
plt.plot(df['month'], df['revenue'])
plt.show()
Terminal Output:
Click "Run File" to execute...

.py file: Run everything, wait for all output at the end

analysis.ipynb
[ ]
import pandas as pd
import matplotlib.pyplot as plt
[ ]
df = pd.read_csv('sales.csv')
df.head()
[ ]
avg = df['revenue'].mean()
print(f"Average: {avg}")

.ipynb file: Click each cell to run it and see output immediately

Try it: On the left, click "Run File" to see the script approach. On the right (notebook), click the cells to run them one by one.

What is a Jupyter Notebook?

A Jupyter Notebook (.ipynb file) consists of cells. Each cell can contain either:

  • Code — Python code that you can run. Output appears directly below the cell.
  • Markdown — Formatted text for explanations, headers, and documentation.

This mix of code and prose makes notebooks ideal for:

  • Exploratory analysis — See results immediately as you work
  • Documentation — Explain your thinking alongside your code
  • Teaching — Students can run code step by step
  • Sharing results — Export to HTML or PDF for reports

When to Use Each

Use .py scripts when... Use .ipynb notebooks when...
Writing production code or libraries Exploring data and trying things out
Building automated pipelines Documenting your analysis step-by-step
Creating reusable functions/classes Teaching or learning new concepts
Running code on a server/schedule Creating reports with visualizations

Local Jupyter vs. Google Colab

Jupyter Notebook and Google Colab are both interactive computing environments for code, but they differ in key ways:

  • Jupyter Notebook is open-source software you install and run locally on your own machine. You have full control over your environment, libraries, and files, but you're limited by your computer's hardware and need to manage dependencies yourself.
  • Google Colab is a cloud-hosted service built on Jupyter that runs in your browser with no setup required. It provides free access to GPUs/TPUs, integrates with Google Drive, and makes sharing easy. However, sessions time out after periods of inactivity, and you're dependent on Google's infrastructure.

You have two main options: run Jupyter locally on your computer, or use Google Colab in the cloud. Each has advantages:

Feature Jupyter (Local) Google Colab
Setup Requires installation Zero setup—runs in browser
Cost Free (uses your hardware) Free tier; Colab Pro for more
GPU Access Only if you have one Free GPU/TPU (limited hours)
File Storage Your local filesystem Google Drive
Packages Full control, install anything Pre-installed; can add more
Collaboration Share files via Git Real-time like Google Docs
Session Length Unlimited Timeout after ~90 min inactivity
Offline Work Yes No—requires internet
Data Privacy Data stays on your machine Goes through Google's servers
Best For Large datasets, sensitive data, long sessions Quick experiments, teaching, ML with GPU
My Recommendation

Start with Google Colab. It requires no setup and is perfect for learning. Move to local Jupyter when you need more control over your environment or are working with sensitive data.

Getting Started with Google Colab

To start with Google Colab just follow these simple steps (for Science Po students: your googe account is your university email):

  1. Go to colab.research.google.com
  2. Sign in with your Google account
  3. Click New Notebook
  4. Start coding!
Google Colab logo
Open Google Colab (Free)

The Colab Interface

Google Colab looks and works almost identically to Jupyter Notebooks:

  • + Code button — Add a new code cell
  • + Text button — Add a new Markdown cell
  • Play button — Run the current cell (or press Shift+Enter)
  • Files panel (left sidebar) — Browse files, mount Google Drive

Connecting to Google Drive

To access files from your Google Drive:

# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')

# Now you can access files like:
import pandas as pd
df = pd.read_csv('/content/drive/MyDrive/data/my_data.csv')

Free GPU for Machine Learning

One of Colab's best features is free access to GPUs for machine learning:

  1. Go to Runtime > Change runtime type
  2. Select T4 GPU (or another available option)
  3. Click Save

This is invaluable when you reach the machine learning modules. Training models that would take hours on your laptop can run in minutes on a GPU.

Installing Jupyter Locally

You have 3 options to get ready to use Jupiter notebooks (my favorite being #3, but I'll walk you through the options):

Option 1: Via pip

Open your computer's terminal (on Mac, hit command+space and type "terminal"), and run:

# Install JupyterLab (recommended)
pip install jupyterlab

# Start JupyterLab
jupyter lab

# Or install classic Jupyter Notebook
pip install notebook
jupyter notebook

Option 2: In VS Code (Recommended)

VS Code can run Jupyter notebooks directly if you have the Jupyter extension installed:

  1. Install the Jupyter extension in VS Code
  2. Create a new file with .ipynb extension
  3. Start coding in cells just like regular Jupyter
VS Code + Jupyter is Great

The reason many researchers prefer running notebooks in VS Code is that you get the best of both worlds: Jupyter's interactivity plus VS Code's powerful editor features, Git integration, and extensions.

Essential Notebook Shortcuts

These shortcuts work in both Jupyter and Colab:

Shortcut Action
Shift + Enter Run cell and move to next
Ctrl + Enter Run cell and stay
Esc Enter command mode (cell border turns blue)
Enter Enter edit mode (cell border turns green)
A (command mode) Insert cell above
B (command mode) Insert cell below
D D (command mode) Delete cell
M (command mode) Convert cell to Markdown
Y (command mode) Convert cell to Code

Your First Notebook

Try this simple example in Colab or Jupyter:

# Cell 1: Import libraries
import pandas as pd
import matplotlib.pyplot as plt

# Cell 2: Create some data
data = {
    'Year': [2020, 2021, 2022, 2023, 2024],
    'Sales': [100, 150, 200, 180, 250]
}
df = pd.DataFrame(data)
df

# Cell 3: Create a plot
plt.figure(figsize=(8, 4))
plt.plot(df['Year'], df['Sales'], marker='o')
plt.title('Sales Over Time')
plt.xlabel('Year')
plt.ylabel('Sales')
plt.show()

Other Cloud Notebook Platforms

While Google Colab is the most popular, there are other options (please let me know if you have favorites that I missed here!):

Platform Best For Link
Google Colab General use, ML with GPU colab.research.google.com
Kaggle Notebooks Data science competitions, datasets kaggle.com/notebooks
Deepnote Team collaboration deepnote.com
Binder Sharing reproducible notebooks mybinder.org
JupyterLite Browser-based, no server jupyter.org/try