Python for Analysts

The Python you actually need for analysis: types, collections, control flow, functions, files and packages.

Beginner · about 15 hours · difficulty 2/5

Python lets you handle data too big or messy for spreadsheets, repeat analyses automatically, and use a huge ecosystem of statistics and visualisation libraries. You do not need to become a software engineer; you need a solid working core.

The core you need

  • Values and types: int, float, str, bool, None. f-strings for formatting: f"{rate:.1%}".
  • Collections: lists, dicts, tuples, sets. Dicts map keys to values and show up everywhere, including JSON from APIs.
  • Control flow: if, for, while, and list or dict comprehensions.
  • Functions: parameters, return values, default arguments. Wrap repeated logic in a function.
  • Files and paths: reading CSV and JSON, pathlib for paths.
  • Errors: read tracebacks from the bottom up; use try/except sparingly.

Environment

  • Install Python via the official installer or a distribution like Anaconda/Miniconda.
  • Use virtual environments (python -m venv .venv) and pip install pandas so each project has its own packages.
  • Write code in VS Code or Jupyter.

Mindset

In analysis you will mostly call libraries rather than write loops. If you find yourself looping over rows of a DataFrame, there is usually a vectorised pandas way.

Pitfall: trying to learn "all of Python" before touching data. Spend a week or two on the basics, then learn the rest through pandas work. Move on when you can write a small script that reads a CSV, filters rows with a function, and writes a summary file.

Key concepts

  • Types, variables, f-strings
  • Lists, dicts, sets, tuples
  • Loops and comprehensions
  • Functions
  • Reading files and JSON
  • Virtual environments and pip
  • Reading tracebacks
  • Calling APIs with requests

Other options for this step

  • R and the Tidyverse: An alternative analysis language with first-class statistics and the tidyverse for readable data pipelines.

Mini project

Write a Python script that downloads a public CSV (e.g. a city's open data portal dataset), counts rows per category using only the standard library, and writes the result to a new CSV.

Check yourself

When would you use a dict instead of a list, and how would you count occurrences of each category with one?

Free resources