Skip to content
Learn Python with BNHR

Start here

On this page
  1. Overview
  2. W · Your first function
  3. R1 · Pad a code
  4. R2 · Codes that are not on the list
  5. R3 · Write a SELECT
  6. R4 · The region of an area code
  7. R5 · Missing or empty
  8. R6 · Make a household
  9. R7 · A number, or nothing
  10. R8 · Where a round's file is

Every exercise in 00_start.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.

Warm-up. Your first function

The task

Task. Finish double(n) so that it returns the number n times two (* multiplies).

call returns
double(21) 42
double(0) 0

If the check is red

Use return, not print.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.1. Pad a code

The task

Task. Finish pad_code(code, width): return the text code, padded on the left with zeros until it is width characters long.

call returns
pad_code("128", 4) "0128"
pad_code("7", 3) "007"

If the check is red

Look at what .zfill does with a code that is already long enough.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.2. Codes that are not on the list

The task

Task. Finish codes_not_listed(round_codes, listed_codes): return the set of codes that are in round_codes but not in listed_codes.

call returns
codes_not_listed({"01", "02", "99"}, {"01", "02", "03"}) {"99"}
codes_not_listed({"01"}, {"01", "02"}) set() (an empty set)

If the check is red

Look at the four operations in the explainer above.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.3. Write a SELECT

The task

Task. Finish select_sql(columns, table): return the SQL text that selects the columns from table.

call returns
select_sql(["hhid", "age"], "round1") "SELECT hhid, age FROM round1"
select_sql(["weight"], "people") "SELECT weight FROM people"

If the check is red

Check the spaces: a comma and one space between columns, one space around SELECT and FROM.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.4. The region of an area code

The task

Task. Finish region_of(area_code, width=2): return the first width characters of area_code. With the default width (2), that is the region.

call returns
region_of("0100305000") "01"
region_of("0100305000", 5) "01003"

If the check is red

Use the input width in your slice, not a fixed number.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.5. Missing or empty

The task

Task. Finish count_missing_or_empty(values): values is a Series; return how many of its values are missing or are the empty text '', as a whole number.

values returns
pd.Series(['01', None, '', '02']) 2
pd.Series(['01', '02']) 0

If the check is red

Count both kinds: missing values and empty texts.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.6. Make a household

The task

Task. Finish make_household(hhid, size): return a Household with the given hhid and size.

call returns
make_household("H00000042", 4) Household(hhid="H00000042", size=4)

If the check is red

Return a Household, not a dictionary or a tuple.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.7. A number, or nothing

The task

Task. Finish to_number(text): return text as a whole number, or None when it is not one.

call returns
to_number("42") 42
to_number("..") None

If the check is red

Catch only ValueError.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Your turn 4.8. Where a round's file is

The task

Task. Finish round_file(round_id): return the path to a round's survey file, which is the file survey.dta in the folder named round_id, inside PATHS.rounds.

call returns (as a path inside the course folder)
round_file("2023Q4") rounds/2023Q4/survey.dta

If the check is red

The result should be a path inside the course folder, ending in survey.dta.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. 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
ModuleA new edition: Round 3 arrives
AboutModule 5 of Python for Data Management: a new edition of the code list stops the command; find out why and map the round back with a crosswalk. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CoursePython for Data Management, free