Type hints and a numeric helper
As projects grow, functions become the way you organise them. A function with type hints states its contract up front:
def standardize(xs: list[float]) -> list[float]:
...
The : list[float] and -> list[float] are type hints, and it's worth being precise about what they do. Python does not enforce them and does not convert anything at runtime — your code runs the same with or without them. What they do is document your intent for human readers and let editors and linters catch mistakes before you run anything, such as passing a single number where a list was expected. They are a comment the tools can check.
Here is a small helper that implements standardisation — subtract the mean, divide by the standard deviation — so a column ends up centred at 0 with a spread of 1:
def standardize(xs: list[float]) -> list[float]:
n = len(xs)
mu = sum(xs) / n
var = sum((x - mu) ** 2 for x in xs) / n
sd = var ** 0.5
return [(x - mu) / sd for x in xs]
Many models expect inputs on a comparable scale, and writing it by hand once means you understand what a library is doing when it offers the same thing ready-made. The first building block is the mean: the sum divided by the count.
leads into “Project paths and feature pipelines”.*
Related cards
Tasks
Card Info
- Topic: Python for Data Science
- Difficulty: Intermediate
- Completed: 0 users