Skip to content
KALANBLON
Show contents

Chapter 6 of 7 · 25 min

Project: a neighbourhood shop's report

By the end of this chapter, you'll be able to

  • Chain the steps of an analysis: read, convert, summarise, show
  • Compute a new column from two others
  • Produce totals per category, per product and per day
  • Present a clear report, with an honest chart

The data

Here is the export of Mariam's spreadsheet for four days (the prices are example prices). Each line is a sale: the date, the product, its category, the quantity sold and the unit price in CFA francs.

date,product,category,quantity,price
2026-11-02,Rice 5 kg,Grocery,4,4500
2026-11-02,Soap,Hygiene,10,350
2026-11-02,Phone credit,Services,6,1000
...

An analysis almost always follows the same steps:

We present the report: a few clear sentences and a bar chart.

Step 4 of 4
Text description of the animation
  1. We read the file: each line becomes a record, but all the values are still text.
  2. We convert the numbers, and compute the amount of each sale.
  3. We add up per category, per product and per day, and summarise.
  4. We present the report: a few clear sentences and a bar chart.

Step 1: read and convert

We read each line, convert the quantity and the price into numbers, and add an amount column: quantity × price. The ready rows go into the sales list.

import csv
import io

data = """date,product,category,quantity,price
2026-11-02,Rice 5 kg,Grocery,4,4500
2026-11-02,Soap,Hygiene,10,350
2026-11-02,Phone credit,Services,6,1000
2026-11-03,Rice 5 kg,Grocery,3,4500
2026-11-03,Oil 1 L,Grocery,5,1500
2026-11-03,Soap,Hygiene,4,350
2026-11-04,Phone credit,Services,12,1000
2026-11-04,Sugar 1 kg,Grocery,6,800
2026-11-05,Oil 1 L,Grocery,2,1500
2026-11-05,Toothpaste,Hygiene,3,900
2026-11-05,Rice 5 kg,Grocery,5,4500"""

sales = []
for row in csv.DictReader(io.StringIO(data)):
    row["quantity"] = int(row["quantity"])
    row["price"] = int(row["price"])
    row["amount"] = row["quantity"] * row["price"]
    sales.append(row)

total = 0
for sale in sales:
    total = total + sale["amount"]

print(len(sales), "sale lines")
print("First sale:", sales[0]["product"], sales[0]["amount"], "CFA francs")
print("Revenue:", total, "CFA francs")
11 sale lines
First sale: Rice 5 kg 18000 CFA francs
Revenue: 94900 CFA francs

Step 2: summarise and show

A single loop fills three dictionaries at once: revenue per category, quantities per product, and revenue per day. Then we look for the best and draw the chart.

import csv
import io
import statistics

data = """date,product,category,quantity,price
2026-11-02,Rice 5 kg,Grocery,4,4500
2026-11-02,Soap,Hygiene,10,350
2026-11-02,Phone credit,Services,6,1000
2026-11-03,Rice 5 kg,Grocery,3,4500
2026-11-03,Oil 1 L,Grocery,5,1500
2026-11-03,Soap,Hygiene,4,350
2026-11-04,Phone credit,Services,12,1000
2026-11-04,Sugar 1 kg,Grocery,6,800
2026-11-05,Oil 1 L,Grocery,2,1500
2026-11-05,Toothpaste,Hygiene,3,900
2026-11-05,Rice 5 kg,Grocery,5,4500"""

by_category = {}
quantities = {}
by_day = {}
for row in csv.DictReader(io.StringIO(data)):
    amount = int(row["quantity"]) * int(row["price"])
    by_category[row["category"]] = by_category.get(row["category"], 0) + amount
    quantities[row["product"]] = quantities.get(row["product"], 0) + int(row["quantity"])
    by_day[row["date"]] = by_day.get(row["date"], 0) + amount

star = ""
for product in quantities:
    if star == "" or quantities[product] > quantities[star]:
        star = product

print("Best-selling product:", star, "-", quantities[star], "units")
print("Median takings per day:", statistics.median(by_day.values()), "CFA francs")

largest = max(by_category.values())
print("Revenue per category, in CFA francs")
for category in by_category:
    length = round(by_category[category] / largest * 30)
    print(category.ljust(9), "█" * length, by_category[category])
Best-selling product: Phone credit - 18 units
Median takings per day: 24950.0 CFA francs
Revenue per category, in CFA francs
Grocery   ██████████████████████████████ 69300
Hygiene   ███ 7600
Services  ████████ 18000

The report for Mariam fits in three sentences: groceries make up nearly three quarters of the revenue; phone credit is the product sold in the largest numbers; a typical day brings in about 25,000 CFA francs of sales.

Your turn

Worked exercise

Worked exercise: the best day

Complete the report: print the revenue of each day, then the best day and the gap with the worst one.

Hint 1
The by_day dictionary from step 2 already holds each day's revenue.
Hint 2
Find the best day like the star product: keep the day with the largest value. Do the same for the worst.
Show the solution and common mistakes
by_day = {"2026-11-02": 27500, "2026-11-03": 22400, "2026-11-04": 16800, "2026-11-05": 28200}

best = ""
worst = ""
for day in by_day:
    print(day, by_day[day], "CFA francs")
    if best == "" or by_day[day] > by_day[best]:
        best = day
    if worst == "" or by_day[day] < by_day[worst]:
        worst = day

print("Best day:", best)
print("Gap with", worst, ":", by_day[best] - by_day[worst], "CFA francs")
2026-11-02 27500 CFA francs
2026-11-03 22400 CFA francs
2026-11-04 16800 CFA francs
2026-11-05 28200 CFA francs
Best day: 2026-11-05
Gap with 2026-11-04 : 11400 CFA francs

Common mistakes:

  • Answering max(by_day.values()): you get the amount 28,200, but not which day achieved it.
  • Comparing dates as numbers: here they are texts. They sort correctly only because they are written year-month-day.

Project: enrich the reportPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Summary

  1. 1An analysis follows four steps: read, convert, summarise, show.
  2. 2A new column (amount = quantity × price) is computed row by row, while reading.
  3. 3A single loop can fill several dictionaries of totals at once.
  4. 4“Best-selling” and “brings in the most” are two different questions; revenue is not profit.
  5. 5A good report fits in a few clear sentences and an honest chart.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.