Skip to content
Learn Python with BNHR

Primer: marimo and the Python this course assumes

On this page
  1. Overview
  2. P1.1 · One line about a household
  3. P1.2 · The mean age, without the code for "not stated"

Every exercise in 90_primer_marimo.py, with the solution and why it works. Each answer stays closed until you swear you tried it first. Opening one changes nothing in your notebook; your check still has to go green from your own code. Read the whole notebook, with its lessons.

Exercise P1.1. One line about a household

The task

Why it matters: this is the whole rhythm of the course's exercises, on Python you know: a dictionary, an f-string and return.

Input → output: a household as a dictionary with "hh_id", "province" and "hh_size" → one line of text about it.

household returns
{"hh_id": "HH-0001", "province": "Ilocos Norte", "hh_size": 7} "HH-0001, Ilocos Norte, household size 7"
{"hh_id": "HH-0412", "province": "Iloilo", "hh_size": 1} "HH-0412, Iloilo, household size 1"

Tools: household["hh_id"] looks up a value by its key; an f-string puts values into text (section 4).

If the check is red: common causes

what you see the usual cause look at
None The function prints the line instead of returning it. return f"..."
Text that differs by a comma or a space The f-string's fixed parts differ from the examples. ", " after the ID and the province, household size before the size
{household['hh_id']} in the result, braces and all The f in front of the text is missing, so nothing is filled in. f"..."
KeyError: 'HH_ID' A key spelled differently from the dictionary's. "hh_id", "province", "hh_size"
The same text for every household The values are typed in instead of looked up. household[...] for each value

Still stuck after ten minutes? Ask your table, then raise your hand.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise P1.2. The mean age, without the code for "not stated"

The task

Why it matters: in the farm survey, an age of 999 means not stated, not an age. A mean that includes it is wrong, with no error to warn you; module 1 shows how far off.

Input → output: a list of ages → the mean of the ages that are not 999, rounded to one decimal; None when no age is stated (all are 999, or the list is empty).

ages returns
[58, 42, 999, 40] 46.7: (58 + 42 + 40) / 3
[999, 999] None

Tools: a list that starts empty, for and if to fill it, .append(value) to add to it; sum(numbers) and len(numbers); round(value, 1); if not stated: is true when the list stated is empty.

If the check is red: common causes

what you see the usual cause look at
284.8 for [58, 42, 999, 40] 999 is counted as an age. keep only the ages that are not 999 before the mean
35.0 for [58, 42, 999, 40] The sum leaves 999 out, but the count does not: 140 is divided by 4. len(stated), not len(ages)
ZeroDivisionError: division by zero No age is stated, so the mean divides by 0. if not stated: return None, before dividing
46.666667 The mean is not rounded. round(..., 1)
None for every list return None runs every time: it is not inside the if, or the if tests the wrong list. the indentation of return None under if not stated:

Still stuck after ten minutes? Ask your table, then raise your hand.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Something new

Identify results
LayerExplore a module
ModuleStart here
AboutModule 1 of Python for Data Management: your laptop ready, how a marimo notebook works, and a refresher of the Python the course uses. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CoursePython for Data Management, free