Type hints and a numeric helper

Intermediate Python for Data Science
Created by Best · 24.06.2026 at 14:03 UTC

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”.*

University approvals: 0
Related cards
Builds on Transpose and the ragged-row pitfalls · Python for Data Science
Next Project paths and feature pipelines · Python for Data Science
Tasks
Question 1

What is the main practical benefit of adding type hints like def f(xs: list[float]) -> float:?

Question 2

Read a line of space-separated floats. Print their mean rounded to 2 decimals.

Example input:

1 2 3 4

Expected output:

2.5
3 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Python for Data Science
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Best
Best
BestBuddy