Python Central HubSign in

Data Analytics with Python / Phase 1: Environment and Setup

Google Colab Walkthrough

Learn Google Colab basics - creating notebooks, uploading data, using GPUs, installing packages, and saving work to Google Drive.

What is Google Colab?

Google Colab (Colaboratory) is a free, cloud-hosted Jupyter Notebook environment.

It allows you to:

  • Run Python in the browser (no local install)
  • Use free GPUs/TPUs (great for ML workloads)
  • Share notebooks easily
  • Connect to Google Drive

For data analytics, Colab is excellent when:

  • Your machine is slow
  • You want a quick setup
  • You want to share a notebook link with others

Create a new notebook

  1. Go to: https://colab.research.google.com/
  2. Sign in with a Google account
  3. Click New notebook

A notebook opens with a default code cell.

Understanding Colab interface

Key menus you’ll use:

  • Runtime: run cells, restart runtime, change hardware
  • File: save a copy, download .ipynb, download .py
  • Edit: find/replace, shortcuts

Colab runs notebooks on remote servers called a runtime.

Run your first code

In a code cell:

Python
print("Hello from Colab")

Run with Shift + Enter.

Uploading datasets

You have a few common options.

Option 1: Upload directly (temporary)

  • Left sidebar → Files
  • Click Upload and choose your CSV

Important:

  • Uploaded files are stored in the runtime temporary disk.
  • If the runtime resets, uploaded files are gone.

Mount your Drive:

Python
from google.colab import drive
drive.mount('/content/drive')

Then access files like:

Python
import pandas as pd
path = "/content/drive/MyDrive/data/titanic.csv"
df = pd.read_csv(path)
df.head()

Option 3: Download from a URL

Python
import pandas as pd
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv"
df = pd.read_csv(url)
df.head()

Installing packages in Colab

Colab has many libraries installed, but not everything.

Use pip inside a notebook:

Python
!pip install yfinance

Or make it a bit quieter:

Python
!pip -q install yfinance

Changing runtime hardware (GPU/TPU)

  1. Runtime → Change runtime type
  2. Set:
    • Hardware accelerator: GPU (or TPU)

You can confirm GPU availability:

Python
import torch
print("CUDA available:", torch.cuda.is_available())

(If you don’t use PyTorch, you can also check with other tools.)

Saving and exporting your work

Save to Drive

  • File → Save a copy in Drive

Download notebook

  • File → Download → .ipynb

Export to Python

  • File → Download → .py

Visualize it

A Colab notebook is the same execute-explore experience as local Jupyter, just running on a remote machine.

diagram Colab runtime lifecycle mermaid
From opening a new notebook to a connected, ready-to-run cloud runtime

Colab tips for data analytics

Quickly preview data

Python
import pandas as pd
import numpy as np
 
df = pd.DataFrame({
    "city": ["Delhi", "Mumbai", "Pune"],
    "sales": [120, 90, 150]
})
 
df

Display full columns/rows (Pandas)

Python
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.max_rows", 100)

Check your understanding

quizCheck yourself
  1. You upload a CSV through the Files sidebar and later the runtime resets. What happens to the file?

    Show answer

    B — It is gone because uploads live on the temporary runtime disk — Directly uploaded files are temporary. Mounting Google Drive is the recommended option for files you want to keep.

  2. Which code mounts Google Drive in Colab?

    Show answer

    A — from google.colab import drive, then drive.mount('/content/drive') — drive.mount('/content/drive') connects Drive, and files then appear under /content/drive/MyDrive.

  3. How do you install a missing package such as yfinance in a Colab cell?

    Show answer

    A — !pip install yfinance — Use the ! shell escape with pip. You may need to reinstall after the runtime restarts.

  4. Where do you switch to a GPU or TPU?

    Show answer

    B — Runtime, then Change runtime type — Runtime, Change runtime type lets you set the hardware accelerator to GPU or TPU.

Next

Continue to: Virtual Environments for Data Science to understand why environments matter and how to maintain clean project dependencies.

🧪 Try It Yourself

Exercise 1 – Build a Google Drive Path

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Read CSV Data From a URL-like String

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Check Whether a Runtime Has a GPU

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did