Data Analyst

Module 1 of 12

Module 1: Python Fundamentals for Data Analysis

6 min read1,082 words
What you'll learn
Write Python code using variables, data types, control flow, and functionsUse list comprehensions and built-in functions to process data efficientlyRead and write data from CSV and JSON filesHandle errors gracefully using try/except blocksBuild small data processing scripts from scratch

"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

TypeEveryday analogyTypical use in data
intCounting whole applesIDs, counts, ages (sometimes)
floatMeasuring weight on a scaleMoney, rates, measurements
strText on a labelNames, categories, raw CSV fields
boolLight switchFlags like “is_active”
NoneEmpty 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

CollectionShapeMutable?Think of it as…
TupleOrderedNoA sealed receipt
SetUnique, unorderedYesA 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: ...).

IdeaWhy it matters
Default argumentsSensible defaults like label="Dataset"
DocstringsFuture-you remembers the contract
Early returnExit 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 for loop 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

TaskPlain English
.strip()Trim whitespace
.lower() / .title()Normalize casing
.split() / .join()CSV-like splitting and rebuilding
f-stringsInsert 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.

python
[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.

PatternUse when
Per-row tryMessy extracts
Outer try around file openWrong 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.

StepPlain English
IngestOpen file with with; use DictReader for headers
ValidateTry convert; append errors list with row id
TransformStrip strings, normalize keys, compute derived fields
AggregateCounters, sums, or defaultdict for grouping
Outputjson.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

  1. Types and collections are your vocabulary; dicts + lists model most data shapes.
  2. Comprehensions and functions keep scripts short and testable.
  3. CSV and JSON cover most loads before Pandas.
  4. Design for skip-and-log, not crash-and-burn.

Resources for Further Learning

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 with and handle try/except around 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.