All posts
postMay 28, 2026

Compute the average from a CSV column

#python#csv#aggregation#challenge
Easy⏱️ Logic Challenge
Given a CSV file "sales.csv" with columns (id, product, price), compute the average price across all rows. Assume the file has at least one row.
Ver solución
import csv def average_price(path): total = 0.0 count = 0 with open(path) as f: for row in csv.DictReader(f): total += float(row["price"]) count += 1 return total / count print(average_price("sales.csv"))

The "load file, compute aggregate" loop is what most data engineers do all day. The challenge here teaches the full pipeline in miniature: open file, iterate rows, convert types (CSV columns are always strings), aggregate, return.

Pay attention to the type conversion — float(row["price"]) — because forgetting it is the most common bug for beginners. Strings concatenate when added; numbers sum.