"Python is the Swiss Army knife of data analysis — simple enough to learn in weeks, powerful enough to use for decades."
Learning Objectives
By the end of this module, you will be able to:
- Write Python code using variables, data types, control flow, and functions
- Use list comprehensions and built-in functions to process data efficiently
- Read and write data from CSV and JSON files
- Handle errors gracefully using try/except blocks
- Build small data processing scripts from scratch
1. Variables and Data Types
Think of a variable as a sticky note on a box: the name is what you call it, the value is what’s inside.
The Core Data Types
| Type | Everyday analogy | Typical use in data |
|---|---|---|
int | Counting whole apples | IDs, counts, ages (sometimes) |
float | Measuring weight on a scale | Money, rates, measurements |
str | Text on a label | Names, categories, raw CSV fields |
bool | Light switch | Flags like “is_active” |
None | Empty slot | “We don’t know yet” |
Type Conversion
Type conversion is like translating languages: CSV numbers often arrive as strings, so you int() or float() them before math. Watch out: int("3.9") fails; int(float("3.9")) works.
Concept: Python figures out types at runtime. For analysis, explicit conversion beats silent bugs.
Fun Fact: Python’s integers can grow huge; you’re unlikely to overflow a counter in normal analytics work.
2. Collections: Lists, Tuples, Dictionaries, Sets
Lists — Your Go-To Collection
Ordered, mutable sequences — like a shopping list you can edit. Index from 0, slice with [start:stop], use negative indices for “from the end.”
Dictionaries — Key-Value Storage
Dicts map keys to values — perfect for JSON-shaped rows. Use .get("key", default) so missing keys don’t crash you.
Tuples and Sets
| Collection | Shape | Mutable? | Think of it as… |
|---|---|---|---|
| Tuple | Ordered | No | A sealed receipt |
| Set | Unique, unordered | Yes | A bowl of unique stickers |
Sets shine for overlap: intersection, union, difference — “customers in Q1 and Q2.”
Try This! Build a dict of three products with price and stock, then compute total inventory value in one expression.
Key Takeaway (section): Lists for sequences, dicts for labeled records, sets for uniqueness.
3. Control Flow
If/Elif/Else
A decision tree: branch on conditions (if / elif / else). Indentation defines blocks — no curly braces.
Loops
for walks a collection; while repeats until a condition fails. enumerate gives index + value; zip pairs columns like zipper teeth.
4. Functions
Reusable recipes: name, parameters, body, return. Define once, call many times — that’s how scripts stay readable.
Lambda Functions — Quick One-Liners
Small anonymous functions, often passed to sorted(..., key=lambda x: ...).
| Idea | Why it matters |
|---|---|
| Default arguments | Sensible defaults like label="Dataset" |
| Docstrings | Future-you remembers the contract |
| Early return | Exit fast on empty input |
5. List Comprehensions
Pattern: [expression for item in iterable if condition]. One line instead of three with append — like a compact assembly line.
- Dict comprehension:
{k: v for ...}. - If logic gets tangled, a plain
forloop is totally fine.
Try This! Clean a list of product names: strip spaces and title-case, in one comprehension.
6. String Operations for Data Cleaning
| Task | Plain English |
|---|---|
.strip() | Trim whitespace |
.lower() / .title() | Normalize casing |
.split() / .join() | CSV-like splitting and rebuilding |
| f-strings | Insert values into messages |
.replace() | Remove dashes or fix separators |
Fun Fact: Many bad joins are just "NYC " vs "NYC".
7. File I/O: Reading and Writing Data
Working with Text Files
open(path, "r") / "w" with with so files always close. Read line-by-line or readlines() for small files.
Working with CSV Files
csv.DictReader yields each row as a dict keyed by header — ideal for cleaning then converting types.
Working with JSON Files
json.load / json.dump for nested API-style data. Great pairing: CSV in from exports, JSON out for apps.
On Windows when writing CSV, use newline="" in open() to avoid extra blank lines.
Key Example: Read employee rows as dicts, normalize names, coerce salary to int, write a JSON list — the bread-and-butter data handoff.
[object Object], csv
,[object Object], json
rows_out = []
,[object Object], ,[object Object],(,[object Object],, ,[object Object],, newline=,[object Object],) ,[object Object], f:
,[object Object], row ,[object Object], csv.DictReader(f):
rows_out.append({
,[object Object],: row[,[object Object],].strip().title(),
,[object Object],: ,[object Object],(row[,[object Object],]),
})
,[object Object], ,[object Object],(,[object Object],, ,[object Object],) ,[object Object], f:
json.dump(rows_out, f, indent=,[object Object],)8. Error Handling
try / except catches failures so one bad value doesn’t kill the whole job. Match specific errors (ValueError, FileNotFoundError) before a broad Exception.
| Pattern | Use when |
|---|---|
| Per-row try | Messy extracts |
| Outer try around file open | Wrong path or permissions |
Try This! Implement safe_float(x) that returns None on bad input instead of raising.
9. Putting It All Together: A Data Processing Script
In plain English: open safely → loop rows → clean strings → convert types → skip or log bad rows → aggregate with dicts or counters → output JSON or print a summary. No framework required.
| Step | Plain English |
|---|---|
| Ingest | Open file with with; use DictReader for headers |
| Validate | Try convert; append errors list with row id |
| Transform | Strip strings, normalize keys, compute derived fields |
| Aggregate | Counters, sums, or defaultdict for grouping |
| Output | json.dump or print summary for humans |
Fun Fact: The collections.Counter and defaultdict types save you from manual “if key in dict” boilerplate in aggregation loops.
Practice Exercises
Exercise 1: Temperature Converter (Beginner)
Convert a list of Fahrenheit values to Celsius with a list comprehension. Formula: (C = (F - 32) \times 5/9).
Exercise 2: Word Frequency Counter (Intermediate)
Given text, return word counts (lowercase, no punctuation), sorted by frequency descending.
Exercise 3: CSV Report Generator (Intermediate)
Read a grades CSV; report average, min, max, and pass count (≥ 60).
Exercise 4: Data Validator (Advanced)
Validate dict records: non-empty name, age 0–150, email has @, phone has 10 digits. Return valid rows and errors.
Exercise 5: JSON Data Merger (Advanced)
Merge customer JSON with orders JSON: per customer, order count and total spend.
Mini-Project: Personal Expense Tracker
CLI tool: load expenses CSV, append new rows, summarize by category and month, export JSON summary using csv + with open.
Key Takeaways
- Types and collections are your vocabulary; dicts + lists model most data shapes.
- Comprehensions and functions keep scripts short and testable.
- CSV and JSON cover most loads before Pandas.
- Design for skip-and-log, not crash-and-burn.
Resources for Further Learning
- Official Python Tutorial
- Automate the Boring Stuff with Python (free online)
- Python for Everybody (free course)
- Real Python — excellent tutorials for all levels
- Python Documentation: csv module
- Python Documentation: json module
Key Takeaway
- Store labeled data in dicts; use lists for ordered sequences.
- Convert types explicitly when ingesting CSV so math behaves.
- Wrap file access in
withand handletry/exceptaround fragile steps. - Extract repeated logic into functions; use comprehensions only while they stay readable.
- Ship small pipelines: read → clean → aggregate → output.
Next up: Module 2 — NumPy: Numerical Computing — where we'll discover why Python + NumPy is 100x faster than raw Python for number crunching.