Day 03 Part 1 — Python for Analytics: Agenda¶
Python is the second language in an analyst's toolkit — and increasingly the first. Where SQL queries a database, Python lets you clean, reshape, automate, visualise, and model data with full programmatic control. This session takes you from zero to writing real analytics code.
Session Overview¶
Duration: 3 hours
Prerequisite: None for Python syntax. SQL Basics helps contextualise the data concepts.
Tools: Python 3.10+, Jupyter Lab (install via pip install -r requirements.txt)
Learning Objectives¶
By the end of this session you will be able to:
- Set up Python and Jupyter and run your first notebook cell
- Use Python's core data types:
int,float,str,bool,list,dict,tuple,set - Write conditional logic with
if/elif/else - Loop over data with
forandwhile - Define reusable functions with
def, parameters, andreturn - Use list comprehensions and dict comprehensions
- Read and write CSV files using Python's standard library
- Process multiple files in a loop with
pathlib - Know where Python fits in the analyst workflow vs SQL and Excel
For mid-level analysts
Even if you've written Python before, pay attention to the file-handling section. The patterns for reading multiple files and writing summary CSVs without Pandas are worth having in your toolkit — they work in restricted environments where you can't install packages.
For senior analysts
The collections module section (in 05-python-for-data-analysis) covers Counter and defaultdict, which are the fast native alternatives to Pandas groupby for simple aggregations. They're useful when Pandas isn't available and when you need to build reusable pipeline components.
Session Flow¶
| Time | Topic | File |
|---|---|---|
| 0:00 – 0:30 | Python environment setup and basics — data types, variables, control flow | 01-python-basics |
| 0:30 – 1:00 | Functions — writing reusable code | 02-functions |
| 1:00 – 1:30 | Lists and dictionaries — Python's core data structures | 03-lists-and-dictionaries |
| 1:30 – 2:00 | File handling — reading and writing CSV data | 04-file-handling |
| 2:00 – 2:30 | Python for data analysis — putting it together | 05-python-for-data-analysis |
| 2:30 – 3:00 | Practice exercises and review | 06-practice-exercises |
Environment Setup¶
Getting Python running is the biggest friction point for beginners. Follow one of these paths — pick only one.
Option 1 — Anaconda (recommended for beginners)¶
Anaconda bundles Python, Jupyter Lab, and all common data science packages into a single installer.
- Download from anaconda.com — choose Python 3.11+
- Run the installer (accept all defaults)
- Open Anaconda Navigator → click Launch under Jupyter Lab
- A browser tab opens with Jupyter. Create a new notebook: File → New → Notebook → Python 3
Test your installation:
Do not install Anaconda and also pip-install packages into base
Mixing Anaconda's conda package manager with pip into the same environment causes dependency conflicts. Use one or the other. If you need a package not in Anaconda, create a new conda environment.
Option 2 — pip + virtual environment (recommended for developers)¶
# Verify Python version
python --version # should be 3.10+
# Create a virtual environment (do this once per project)
python -m venv .venv
# Activate it
source .venv/bin/activate # Mac/Linux
.venv\Scripts\activate # Windows PowerShell
# Install packages
pip install -r requirements.txt
# Launch Jupyter Lab
jupyter lab
Option 3 — Google Colab (no local install needed)¶
Go to colab.research.google.com. Sign in with a Google account. Create a new notebook. All packages used in this course are pre-installed on Colab.
Colab for quick experiments
Colab is ideal for following along with course notebooks without any local setup. The only limitation is that files you create in a session are not persisted after the session closes — download them before closing.
Jupyter Lab — Essential Operations¶
Running cells¶
| Action | Keyboard Shortcut |
|---|---|
| Run cell and stay | Ctrl + Enter |
| Run cell and move to next | Shift + Enter |
| Run all cells | Ctrl + Shift + Enter (Colab) / Run All in menu |
| Add cell below | B (in command mode) |
| Add cell above | A (in command mode) |
| Delete cell | DD (press D twice in command mode) |
| Switch to command mode | Esc |
| Switch to edit mode | Enter |
| Convert cell to Markdown | M (in command mode) |
| Undo last action | Z (command mode) / Ctrl+Z (edit mode) |
Command mode vs Edit mode
In Edit mode (green border), keystrokes type into the cell. In Command mode (blue border), single keystrokes are shortcuts. Press Esc to switch to command mode, Enter to switch to edit mode.
Markdown cells¶
Use Markdown cells to document your analysis. In a Markdown cell:
- # Heading, ## Subheading for titles
- **bold**, *italic*
- `code` for inline code
- - item for bullet lists
Magic commands¶
%timeit sum(range(1000)) # time a statement
%time df.groupby("cat").sum() # time a single expression
%%time # time an entire cell
%who # list variables in namespace
%matplotlib inline # show plots inline (Jupyter Notebook)
!pip install somepackage # run shell command from cell
Libraries You Need for This Session¶
# requirements.txt — install before starting
pandas==2.2.0
numpy==1.26.4
matplotlib==3.8.2
seaborn==0.13.2
openpyxl==3.1.2 # for Excel reading/writing
jupyter lab
# Verification — run this cell to confirm everything is installed
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
print(f"pandas {pd.__version__}")
print(f"numpy {np.__version__}")
print(f"matplotlib {plt.matplotlib.__version__}")
print("All imports successful.")
Where Python Fits in the Analytics Workflow¶
Understanding which tool to use for each task prevents a lot of wasted effort.
| Task | Best Tool | Why |
|---|---|---|
| Query a relational database | SQL | Pushes work to the database — fast and memory-efficient |
| Quick ad-hoc pivot | Excel | Fastest for small, one-off tables |
| Clean messy data at scale | Python (Pandas) | Repeatable, scriptable, handles any size |
| Automate a repetitive report | Python | Run once, schedule, never touch again |
| Build a calculated column with complex logic | Python | Easier than SQL window functions for multi-step logic |
| Plot a chart with full visual control | Python (matplotlib/seaborn) | 100% customisable |
| Build a machine learning model | Python (scikit-learn) | Only viable option |
| Share a dashboard with non-technical stakeholders | Power BI / Tableau | Drag-and-drop, sharable links |
| Process a 10GB CSV file | Python (Pandas with chunking or Polars) | Excel and SQL struggle here |
The analyst's decision rule
If you're answering a one-time question with clean data already in a database → SQL first. If you're building a repeatable process, cleaning data, or doing anything more than basic aggregation → Python. If the audience needs to click and explore → a BI tool.
Common Setup Errors and Fixes¶
| Error | Cause | Fix |
|---|---|---|
ModuleNotFoundError: No module named 'pandas' |
Package not installed in active environment | Run pip install pandas in the same terminal where you launched Jupyter |
jupyter: command not found |
Jupyter not installed | pip install jupyterlab |
IndentationError |
Mixed tabs and spaces | Use 4 spaces consistently. In VS Code: Settings → "Detect Indentation" → off, set to 4 spaces |
| Kernel keeps dying | Out of memory | Restart kernel, process data in smaller chunks |
| Cell runs but nothing prints | Forgot print() |
In Jupyter, the last expression in a cell auto-displays. For intermediate values, use print() |
UnicodeDecodeError on CSV |
File saved with Windows encoding | pd.read_csv("file.csv", encoding="cp1252") |