Module 2 · Python for Data · Lesson 7 of 51
Mini project: a sales report in plain Python
Read the café's CSV file line by line, convert types, total sales by branch and payment, and print a formatted report; then see why pandas exists.
In this lesson · about 40 minutes
- Read a CSV file with the csv module
- Combine loops, dictionaries and functions into a small report
- Explain what pandas will do for you, and why
Reading a CSV
A CSV (comma-separated values) file is a table as plain text: the first line holds column names, every other line is a row. Python's csv module reads it. csv.DictReader gives each row as a dictionary keyed by column name, with every value as text, so numbers need converting.
import csv
with open("kc_orders.csv") as f:
rows = list(csv.DictReader(f))
rows[0]["total"] # '4.5', a string
with open(...) as f: opens the file and closes it automatically when the block ends.
Revenue by branch, by hand
Exercise
Average order by payment method
Using rows (already read for you), build avg_by_payment: a dictionary mapping each payment method to its average order total, rounded to 2 decimals. Hint: keep two dictionaries, one of sums and one of counts.
Code that runs before yours
import csv
with open("kc_orders.csv") as f:
rows = list(csv.DictReader(f))The same report with pandas (preview)
Flashcards
Module 2 flashcards
0 of 9 known
Say the answer out loud, flip the card, then be honest: cards you miss come back at the end.
Practice exam
Module 2 checkpoint
10 questions, 12 minutes.
- 10 questions · 12 minutes
- No feedback until you submit. Flag questions to come back to them.
- Aim for 85% or better before you move on. The real exam's 750 out of 900 is roughly 80–83%.
Exercises solved: 0 of 1
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.