import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Start%20here%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first%20and%20holds%20what%20the%20other%20cells%20share%3A%20the%20imports%20and%20two%20lists.%0A%20%20%20%20import%20ast%0A%20%20%20%20import%20html%0A%20%20%20%20import%20importlib.metadata%0A%20%20%20%20import%20platform%0A%20%20%20%20import%20sys%0A%20%20%20%20from%20pathlib%20import%20PureWindowsPath%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20pydantic%20import%20BaseModel%2C%20ValidationError%0A%0A%20%20%20%20import%20course%0A%20%20%20%20from%20course%20import%20PATHS%2C%20check_cases%2C%20code_view%2C%20pick_first%0A%0A%20%20%20%20PACKAGES%20%3D%20%5B%22marimo%22%2C%20%22duckdb%22%2C%20%22pandas%22%2C%20%22pyreadstat%22%2C%20%22openpyxl%22%2C%20%22pydantic%22%2C%20%22pandera%22%2C%20%22polars%22%5D%0A%20%20%20%20COURSE_FILES%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22rounds%2F2023Q4%2Fsurvey.dta%22%2C%0A%20%20%20%20%20%20%20%20%22reference%2Fpsgc_2023Q4.xlsx%22%2C%0A%20%20%20%20%20%20%20%20%22reference%2Fpsgc_2024Q2.xlsx%22%2C%0A%20%20%20%20%20%20%20%20%22reference%2Flegacy_system.sqlite%22%2C%0A%20%20%20%20%20%20%20%20%22reference%2Fopenstat_population_wide.csv%22%2C%0A%20%20%20%20%20%20%20%20%22reference%2Fopenstat_api%2Fpage-1.json%22%2C%0A%20%20%20%20%20%20%20%20%22reference%2Fopenstat_api%2Fpage-2.json%22%2C%0A%20%20%20%20%20%20%20%20%22checks.json%22%2C%0A%20%20%20%20%20%20%20%20%2210_get_round.py%22%2C%0A%20%20%20%20%20%20%20%20%2290_primers.py%22%2C%0A%20%20%20%20%20%20%20%20%2291_glossary.py%22%2C%0A%20%20%20%20%20%20%20%20%22explain.py%22%2C%0A%20%20%20%20%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Start%20here%0A%0A%20%20%20%20Welcome%20to%20**Python%20for%20Data%20Management**.%20This%20notebook%20gets%20you%20ready%20for%20the%20course%3A%0A%0A%20%20%20%201.%20it%20checks%20that%20your%20computer%20is%20ready%3B%0A%20%20%20%202.%20it%20shows%20how%20a%20marimo%20notebook%20works%3B%0A%20%20%20%203.%20it%20explains%20the%20Python%20ideas%20everything%20else%20is%20built%20on%20(values%2C%20variables%2C%20comments%2C%20functions)%2C%20and%20how%20the%20exercises%20work%3B%0A%20%20%20%204.%20it%20refreshes%20the%20Python%20you%20will%20use%2C%20one%20small%20exercise%20at%20a%20time.%0A%0A%20%20%20%20Work%20from%20top%20to%20bottom.%20Wherever%20you%20see%20a%20slider%2C%20a%20box%20or%20a%20dropdown%2C%20play%20with%20it%3A%20the%20explanation%20under%20it%20changes%20as%20you%20do.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%221.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Is%20your%20computer%20ready%3F%0A%0A%20%20%20%20Each%20line%20below%20is%20one%20check.%20If%20a%20line%20says%20**no**%2C%20read%20the%20last%20column%2C%20which%20says%20what%20to%20do%3B%20if%20it%20still%20fails%2C%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20environment_report()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20check%3A%20what%20was%20checked%2C%20whether%20it%20is%20ready%2C%20and%20what%20to%20do%20if%20not.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20add(what%3A%20str%2C%20ready%3A%20bool%2C%20detail%3A%20str%2C%20fix%3A%20str%20%3D%20%22%22)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20rows.append(%7B%22check%22%3A%20what%2C%20%22ready%22%3A%20%22yes%22%20if%20ready%20else%20%22no%22%2C%20%22detail%22%3A%20detail%2C%20%22if%20not%20ready%22%3A%20%22%22%20if%20ready%20else%20fix%7D)%0A%0A%20%20%20%20%23%201.%20The%20Python%20version%20that%20uv%20installed%20for%20the%20course%0A%20%20%20%20add(%22Python%203.12%20or%20newer%22%2C%20sys.version_info%20%3E%3D%20(3%2C%2012)%2C%20platform.python_version()%2C%0A%20%20%20%20%20%20%20%20%22Close%20this%20window%2C%20then%20start%20again%20from%20the%20course%20folder%20with%3A%20uv%20run%20marimo%20edit%22)%0A%20%20%20%20%23%202.%20Every%20package%20the%20course%20uses%0A%20%20%20%20for%20name%20in%20PACKAGES%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20add(f%22package%20%7Bname%7D%22%2C%20True%2C%20importlib.metadata.version(name))%0A%20%20%20%20%20%20%20%20except%20importlib.metadata.PackageNotFoundError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20add(f%22package%20%7Bname%7D%22%2C%20False%2C%20%22not%20installed%22%2C%20%22In%20the%20course%20folder%2C%20run%3A%20uv%20sync%22)%0A%20%20%20%20%23%203.%20The%20course's%20data%20files%0A%20%20%20%20missing%20%3D%20%5Bf%20for%20f%20in%20COURSE_FILES%20if%20not%20(PATHS.root%20%2F%20f).exists()%5D%0A%20%20%20%20add(%22course%20data%20files%22%2C%20not%20missing%2C%20f%22%7Blen(COURSE_FILES)%20-%20len(missing)%7D%20of%20%7Blen(COURSE_FILES)%7D%20found%22%2C%0A%20%20%20%20%20%20%20%20%22Missing%3A%20%22%20%2B%20%22%2C%20%22.join(missing)%20%2B%20%22.%20Download%20the%20course%20folder%20again%2C%20and%20unzip%20all%20of%20it.%22)%0A%20%20%20%20%23%204.%20Whether%20you%20can%20save%20files%20in%20the%20course%20folder%20(the%20notebooks%20save%20your%20configuration%20there)%0A%20%20%20%20probe%20%3D%20PATHS.config%20%2F%20%22.write-test%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20probe.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20%20%20%20%20probe.write_text(%22ok%22%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20%20%20%20%20probe.unlink()%0A%20%20%20%20%20%20%20%20add(%22can%20save%20files%20in%20the%20course%20folder%22%2C%20True%2C%20PATHS.root.as_posix())%0A%20%20%20%20except%20OSError%20as%20error%3A%0A%20%20%20%20%20%20%20%20add(%22can%20save%20files%20in%20the%20course%20folder%22%2C%20False%2C%20str(error)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Move%20the%20course%20folder%20somewhere%20you%20can%20write%2C%20such%20as%20Documents%20(not%20inside%20a%20zip%2C%20not%20a%20network%20drive).%22)%0A%20%20%20%20%23%205.%20DuckDB's%20sqlite%20extension%3A%20downloaded%20once%2C%20used%20in%20the%20Get%20module%20to%20read%20an%20older%20system's%20database%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20con.execute(%22INSTALL%20sqlite%22)%0A%20%20%20%20%20%20%20%20con.execute(%22LOAD%20sqlite%22)%0A%20%20%20%20%20%20%20%20add(%22DuckDB's%20sqlite%20extension%22%2C%20True%2C%20%22installed%22)%0A%20%20%20%20except%20duckdb.Error%20as%20error%3A%0A%20%20%20%20%20%20%20%20add(%22DuckDB's%20sqlite%20extension%22%2C%20False%2C%20str(error).splitlines()%5B0%5D%5B%3A120%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22It%20downloads%20once.%20Connect%20to%20the%20internet%20and%20press%20'Check%20again'.%20The%20course%20still%20works%20without%20it%2C%20more%20slowly.%22)%0A%20%20%20%20finally%3A%0A%20%20%20%20%20%20%20%20con.close()%0A%20%20%20%20return%20pd.DataFrame(rows)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20recheck%20%3D%20mo.ui.run_button(label%3D%22Check%20again%22)%0A%20%20%20%20recheck%0A%20%20%20%20return%20(recheck%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(recheck)%3A%0A%20%20%20%20recheck%20%20%23%20pressing%20the%20button%20runs%20this%20cell%20again%0A%20%20%20%20_report%20%3D%20environment_report()%0A%20%20%20%20_not_ready%20%3D%20_report%5B_report%5B%22ready%22%5D%20%3D%3D%20%22no%22%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22**Your%20computer%20is%20ready.**%22%20if%20_not_ready.empty%20else%20f%22**%7Blen(_not_ready)%7D%20check(s)%20need%20attention.**%20Read%20the%20last%20column.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20kind%3D%22success%22%20if%20_not_ready.empty%20else%20%22warn%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(_report%2C%20pagination%3DFalse%2C%20selection%3DNone)%2C%20%20%23%20every%20check%20on%20one%20page%2C%20so%20a%20%22no%22%20cannot%20hide%20on%20page%202%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20How%20a%20marimo%20notebook%20works%0A%0A%20%20%20%20A%20notebook%20is%20a%20list%20of%20**cells**.%20A%20cell%20holds%20Python%20code%3B%20its%20result%20appears%20**above**%20the%20code.%0A%0A%20%20%20%20-%20**Run%20a%20cell**%3A%20click%20inside%20it%20and%20press%20**Ctrl%20%2B%20Enter**%20(Mac%3A%20**Cmd%20%2B%20Enter**)%2C%20or%20click%20the%20%E2%96%B6%20button%20beside%20it.%0A%20%20%20%20-%20**Show%20the%20code**%20of%20a%20cell%20that%20only%20shows%20text%20or%20a%20result%3A%20hover%20over%20it%20and%20click%20the%20%60%3C%2F%3E%60%20icon.%20Many%20cells%20hide%20their%20code%20so%20the%20page%20stays%20readable%3B%20you%20can%20always%20look.%0A%20%20%20%20-%20**Cells%20update%20each%20other.**%20When%20you%20change%20a%20cell%20and%20run%20it%2C%20every%20cell%20that%20uses%20its%20results%20runs%20again%20by%20itself.%20You%20never%20have%20to%20remember%20an%20order%20to%20run%20things%20in.%0A%20%20%20%20-%20**Each%20name%20lives%20in%20one%20cell.**%20If%20one%20cell%20says%20%60total%20%3D%205%60%2C%20no%20other%20cell%20may%20also%20create%20%60total%60%3B%20marimo%20will%20say%20*%22multiple%20definitions%22*.%20Use%20a%20new%20name%2C%20or%20start%20a%20name%20with%20an%20underscore%20(%60_total%60)%20to%20keep%20it%20private%20to%20its%20cell.%0A%20%20%20%20-%20**Your%20work%20is%20saved**%20automatically.%20To%20be%20sure%2C%20press%20**Ctrl%20%2B%20S**%20(Mac%3A%20**Cmd%20%2B%20S**).%0A%0A%20%20%20%20Try%20it%3A%20move%20the%20slider.%20The%20table%20under%20it%20updates%20by%20itself%2C%20because%20its%20cell%20uses%20%60rows.value%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20A%20slider%20is%20a%20UI%20element.%20Its%20current%20value%20is%20rows.value.%0A%20%20%20%20rows%20%3D%20mo.ui.slider(start%3D1%2C%20stop%3D8%2C%20value%3D3%2C%20label%3D%22How%20many%20area%20codes%20to%20show%22%2C%20show_value%3DTrue)%0A%20%20%20%20rows%0A%20%20%20%20return%20(rows%2C)%0A%0A%0A%40app.cell%0Adef%20_(rows)%3A%0A%20%20%20%20%23%20This%20cell%20uses%20rows.value%2C%20so%20it%20runs%20again%20whenever%20you%20move%20the%20slider.%0A%20%20%20%20codes%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22area_code%22%3A%20%5B%220100105000%22%2C%20%220100205000%22%2C%20%220100305000%22%2C%20%220200105000%22%2C%20%220300105000%22%2C%20%220400105000%22%2C%20%220500105000%22%2C%20%220600105000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22persons%22%3A%20%5B212%2C%20187%2C%20240%2C%20199%2C%20305%2C%20266%2C%20151%2C%20140%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20codes.head(rows.value)%20%20%23%20the%20last%20line%20of%20a%20cell%20is%20what%20it%20shows%0A%20%20%20%20return%20(codes%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20cell%20can%20also%20be%20a%20**SQL%20cell**%2C%20written%20in%20SQL%20instead%20of%20Python.%20It%20can%20query%20any%20table%20in%20the%20notebook%20by%20its%20name.%20The%20one%20below%20adds%20up%20%60persons%60%20per%20region%20of%20%60codes%60%20(the%20region%20is%20the%20first%20two%20characters%20of%20the%20area%20code).%20Move%20the%20slider%20above%3A%20this%20result%20does%20not%20change%2C%20because%20it%20uses%20all%20of%20%60codes%60%2C%20not%20the%20rows%20shown.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(codes)%3A%0A%20%20%20%20by_region%20%3D%20mo.sql(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20SELECT%20substr(area_code%2C%201%2C%202)%20AS%20region%2C%20sum(persons)%20AS%20persons%0A%20%20%20%20%20%20%20%20FROM%20codes%0A%20%20%20%20%20%20%20%20GROUP%20BY%20region%0A%20%20%20%20%20%20%20%20ORDER%20BY%20region%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Python%20foundations%0A%0A%20%20%20%20Everything%20in%20the%20course%20is%20built%20from%20a%20handful%20of%20ideas.%20If%20you%20have%20programmed%20before%2C%20read%20this%20quickly%3B%20if%20not%2C%20take%20your%20time.%20There%20is%20nothing%20to%20get%20wrong%20here.%0A%0A%20%20%20%20%23%23%23%203.1%20Values%20and%20their%20types%0A%0A%20%20%20%20A%20**value**%20is%20a%20piece%20of%20data%3A%20a%20number%2C%20a%20text%2C%20a%20yes%2Fno.%20Every%20value%20has%20a%20**type**%2C%20which%20decides%20what%20you%20can%20do%20with%20it.%20Text%20is%20written%20between%20quotes%3A%20%60%220128%22%60%20is%20text%2C%20%60128%60%20is%20a%20number.%20That%20difference%20is%20the%20whole%20first%20lesson%20of%20the%20Get%20module.%0A%0A%20%20%20%20Type%20a%20value%20in%20the%20box%20(try%20%60128%60%2C%20%60%220128%22%60%2C%20%603.5%60%2C%20%60True%60%2C%20%60None%60%2C%20%60%5B1%2C%202%5D%60%2C%20%60%7B%22a%22%3A%201%7D%60%2C%20or%20%600128%60%20without%20quotes)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20type_box%20%3D%20mo.ui.text(value%3D'%220128%22'%2C%20label%3D%22A%20Python%20value%3A%22)%0A%20%20%20%20type_box%0A%20%20%20%20return%20(type_box%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(type_box)%3A%0A%20%20%20%20_notes%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22int%22%3A%20%22a%20whole%20number.%20You%20can%20add%20and%20compare%20it%3B%20leading%20zeros%20are%20not%20allowed.%22%2C%0A%20%20%20%20%20%20%20%20%22float%22%3A%20%22a%20decimal%20number.%20%60100305000.0%60%20is%20a%20float%2C%20even%20though%20it%20ends%20in%20%60.0%60.%22%2C%0A%20%20%20%20%20%20%20%20%22str%22%3A%20%22text%20(a%20*string*).%20It%20keeps%20every%20character%2C%20leading%20zeros%20included.%22%2C%0A%20%20%20%20%20%20%20%20%22bool%22%3A%20%22yes%20or%20no%3A%20%60True%60%20or%20%60False%60.%20Comparisons%20such%20as%20%605%20%3E%203%60%20give%20one.%22%2C%0A%20%20%20%20%20%20%20%20%22NoneType%22%3A%20%22%60None%60%2C%20Python's%20way%20of%20saying%20*no%20value*.%20Missing%20data%20often%20becomes%20%60None%60.%22%2C%0A%20%20%20%20%20%20%20%20%22list%22%3A%20%22an%20ordered%20list%20of%20values%2C%20in%20square%20brackets.%22%2C%0A%20%20%20%20%20%20%20%20%22set%22%3A%20%22a%20collection%20where%20each%20value%20appears%20once%2C%20in%20curly%20brackets.%22%2C%0A%20%20%20%20%20%20%20%20%22dict%22%3A%20%22a%20dictionary%3A%20keys%20mapped%20to%20values%2C%20like%20%60%7B%5C%22area_code%5C%22%3A%20%5C%22psgc%5C%22%7D%60.%22%2C%0A%20%20%20%20%20%20%20%20%22tuple%22%3A%20%22a%20fixed%20group%20of%20values%2C%20in%20round%20brackets%2C%20such%20as%20a%20pair%20%60(1842300%2C%20%5C%22published%5C%22)%60.%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_value%20%3D%20ast.literal_eval(type_box.value)%20%20%23%20reads%20a%20Python%20value%20from%20text%2C%20safely%0A%20%20%20%20%20%20%20%20_type%20%3D%20type(_value).__name__%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.md(f%22%60%60%60python%5Cnx%20%3D%20%7Btype_box.value%7D%5Cntype(x)%20%20%20%23%20%7B_type%7D%5Cn%60%60%60%5CnPython%20reads%20that%20as%20a%20**%60%7B_type%7D%60**%3A%20%7B_notes.get(_type%2C%20'a%20value%20of%20this%20type.')%7D%22)%0A%20%20%20%20except%20(ValueError%2C%20SyntaxError)%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.md(f%22%60%60%60python%5Cnx%20%3D%20%7Btype_box.value%7D%5Cn%60%60%60%5CnThat%20is%20**not%20a%20valid%20Python%20value**%20(%7Btype(_error).__name__%7D).%20Text%20needs%20quotes%20around%20it%3B%20and%20a%20number%20cannot%20start%20with%20%600%60%2C%20which%20is%20one%20more%20reason%20codes%20are%20text.%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.2%20Variables%3A%20names%20for%20values%0A%0A%20%20%20%20A%20**variable**%20is%20a%20name%20that%20points%20to%20a%20value.%20You%20create%20one%20with%20%60%3D%60%2C%20which%20means%20*%22from%20now%20on%2C%20this%20name%20means%20this%20value%22*%20(it%20is%20not%20the%20%60%3D%60%20of%20mathematics).%20Read%20it%20from%20right%20to%20left%3A%20compute%20the%20right%20side%2C%20then%20give%20it%20the%20name%20on%20the%20left.%0A%0A%20%20%20%20The%20cell%20below%20is%20ordinary%20code%20you%20can%20change.%20Change%20%605%60%20to%20%606%60%2C%20run%20it%20(Ctrl%20%2B%20Enter)%2C%20and%20watch%20%60adults%60%20change.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20household_size%20%3D%205%20%20%23%20the%20name%20household_size%20now%20points%20to%20the%20value%205%0A%20%20%20%20children%20%3D%202%0A%20%20%20%20adults%20%3D%20household_size%20-%20children%20%20%23%20the%20right%20side%20is%20computed%20first%2C%20then%20named%20adults%0A%20%20%20%20adults%20%20%23%20the%20last%20line%20of%20a%20cell%20is%20shown%20above%20it%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.3%20Comments%2C%20and%20%22commenting%20out%22%0A%0A%20%20%20%20Everything%20after%20a%20%60%23%60%20on%20a%20line%20is%20a%20**comment**%3A%20Python%20skips%20it.%20Comments%20are%20notes%20for%20people%3A%20what%20a%20line%20does%2C%20or%20why.%20You%20will%20see%20many%20in%20this%20course.%0A%0A%20%20%20%20**Commenting%20out**%20means%20putting%20%60%23%60%20in%20front%20of%20a%20line%20of%20code%20to%20switch%20it%20off%2C%20without%20deleting%20it.%20In%20the%20cell%20below%2C%20the%20line%20%60total%20%3D%200%60%20is%20commented%20out.%20Remove%20only%20the%20**first**%20%60%23%60%20on%20that%20line%20(the%20one%20before%20%60total%60)%2C%20run%20the%20cell%2C%20and%20%60total%60%20becomes%20%600%60.%20The%20rest%20of%20the%20line%20stays%20a%20comment%2C%20because%20it%20starts%20with%20its%20own%20%60%23%60.%20Then%20put%20the%20first%20%60%23%60%20back.%20(In%20marimo%2C%20**Ctrl%20%2B%20%2F**%2C%20or%20**Cmd%20%2B%20%2F**%20on%20a%20Mac%2C%20comments%20or%20uncomments%20the%20selected%20lines.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20This%20whole%20line%20is%20a%20comment%3A%20Python%20skips%20it.%0A%20%20%20%20total%20%3D%2010%20%2B%205%20%20%23%20a%20comment%20can%20also%20follow%20code%20on%20the%20same%20line%0A%20%20%20%20%23%20total%20%3D%200%20%20%20%20%20%23%20%3C-%20this%20line%20is%20%22commented%20out%22%3A%20it%20does%20nothing%20until%20you%20remove%20the%20first%20%23%0A%20%20%20%20total%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.4%20Blocks%3A%20%60if%60%2C%20and%20why%20indentation%20matters%0A%0A%20%20%20%20%60if%60%20runs%20some%20lines%20only%20when%20a%20condition%20is%20true.%20%60elif%60%20(*else%20if*)%20tries%20another%20condition%2C%20and%20%60else%60%20catches%20everything%20left.%20The%20lines%20that%20belong%20to%20each%20part%20are%20**indented**%20(pushed%20right%20by%204%20spaces).%20In%20Python%20the%20indentation%20is%20not%20decoration%3A%20it%20is%20how%20Python%20knows%20which%20lines%20belong%20to%20which%20part.%20The%20same%20goes%20for%20%60for%60%20loops%2C%20functions%2C%20and%20%60try%60.%0A%0A%20%20%20%20In%20the%20Get%20module%20you%20will%20read%20published%20tables%20where%20a%20cell%20can%20be%20a%20number%2C%20%60..%60%20(not%20available)%20or%20%60-%60%20(not%20applicable).%20Type%20a%20cell%20value%20in%20the%20box%20and%20watch%20which%20lines%20run%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20cell_box%20%3D%20mo.ui.text(value%3D%221%2C842%2C300%22%2C%20label%3D%22A%20published%20cell%3A%22)%0A%20%20%20%20cell_box%0A%20%20%20%20return%20(cell_box%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(cell_box)%3A%0A%20%20%20%20_text%20%3D%20cell_box.value.strip()%0A%20%20%20%20_branch%20%3D%200%20if%20_text%20%3D%3D%20%22..%22%20else%201%20if%20_text%20%3D%3D%20%22-%22%20else%202%0A%20%20%20%20_status%20%3D%20(%22not%20available%22%2C%20%22not%20applicable%22%2C%20%22published%22)%5B_branch%5D%0A%20%20%20%20code_view(%0A%20%20%20%20%20%20%20%20%5B(f'text%20%3D%20%22%7B_text%7D%22'%2C%20True)%2C%0A%20%20%20%20%20%20%20%20%20('if%20text%20%3D%3D%20%22..%22%3A'%2C%20_branch%20%3D%3D%200)%2C%20('%20%20%20%20status%20%3D%20%22not%20available%22'%2C%20_branch%20%3D%3D%200)%2C%0A%20%20%20%20%20%20%20%20%20('elif%20text%20%3D%3D%20%22-%22%3A'%2C%20_branch%20%3D%3D%201)%2C%20('%20%20%20%20status%20%3D%20%22not%20applicable%22'%2C%20_branch%20%3D%3D%201)%2C%0A%20%20%20%20%20%20%20%20%20(%22else%3A%22%2C%20_branch%20%3D%3D%202)%2C%20('%20%20%20%20status%20%3D%20%22published%22'%2C%20_branch%20%3D%3D%202)%5D%2C%0A%20%20%20%20%20%20%20%20caption%3Df%22Only%20the%20highlighted%20lines%20run%2C%20so%20%3Ccode%3Estatus%3C%2Fcode%3E%20is%20%3Cb%3E%5C%22%7Bhtml.escape(_status)%7D%5C%22%3C%2Fb%3E.%20Try%20%3Ccode%3E..%3C%2Fcode%3E%2C%20then%20%3Ccode%3E-%3C%2Fcode%3E.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.5%20Functions%3A%20give%20steps%20a%20name%2C%20take%20inputs%2C%20return%20a%20result%0A%0A%20%20%20%20A%20**function**%20packages%20steps%20so%20you%20can%20use%20them%20again%2C%20on%20any%20input.%20Every%20exercise%20in%20this%20course%20is%20a%20function%2C%20so%20this%20is%20worth%20reading%20twice.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20def%20pad_code(code%2C%20width)%3A%20%20%20%20%20%20%20%20%23%201.%20def%2C%20the%20function's%20name%2C%20its%20inputs%20in%20brackets%2C%20and%20a%20colon%0A%20%20%20%20%20%20%20%20padded%20%3D%20code.zfill(width)%20%20%20%20%23%202.%20the%20steps%2C%20indented%20under%20def%0A%20%20%20%20%20%20%20%20return%20padded%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%203.%20return%3A%20the%20result%20handed%20back%20to%20whoever%20called%20the%20function%0A%0A%20%20%20%20pad_code(%22128%22%2C%204)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%204.%20calling%20the%20function%3A%20its%20inputs%20get%20these%20values%2C%20and%20the%20call%20gives%20%220128%22%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20In%20the%20course's%20functions%2C%20the%20first%20line%20under%20%60def%60%20is%20often%20a%20text%20in%20triple%20quotes%3A%20a%20**docstring**%2C%20describing%20what%20the%20function%20does.%20It%20is%20there%20for%20people%3B%20Python%20does%20not%20run%20it.%0A%0A%20%20%20%20-%20**Inputs**%20(also%20called%20*parameters*)%3A%20%60code%60%20and%20%60width%60%20are%20names%20that%2C%20inside%20the%20function%2C%20point%20to%20whatever%20values%20the%20caller%20gave.%20Calling%20%60pad_code(%22128%22%2C%204)%60%20runs%20the%20steps%20with%20%60code%20%3D%20%22128%22%60%20and%20%60width%20%3D%204%60.%0A%20%20%20%20-%20**%60return%60**%20ends%20the%20function%20and%20hands%20its%20result%20back.%20The%20call%20%60pad_code(%22128%22%2C%204)%60%20*becomes*%20that%20result%2C%20so%20you%20can%20name%20it%3A%20%60result%20%3D%20pad_code(%22128%22%2C%204)%60.%20A%20function%20without%20%60return%60%20gives%20back%20%60None%60.%0A%20%20%20%20-%20**%60print%60%20is%20not%20%60return%60.**%20%60print%60%20only%20shows%20text%20on%20the%20screen%3B%20the%20caller%20gets%20nothing%20it%20can%20use.%20The%20exercise%20checks%20look%20at%20what%20your%20function%20**returns**.%0A%20%20%20%20-%20An%20input%20can%20have%20a%20**default**%2C%20used%20when%20the%20caller%20leaves%20it%20out%3A%20%60def%20pad_code(code%2C%20width%3D10)%60%20makes%20%60pad_code(%22128%22)%60%20mean%20%60pad_code(%22128%22%2C%2010)%60.%0A%0A%20%20%20%20Change%20the%20inputs%20and%20follow%20the%20values%20through%20the%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20fn_code%20%3D%20mo.ui.text(value%3D%22128%22%2C%20label%3D%22code%3A%22)%0A%20%20%20%20fn_width%20%3D%20mo.ui.slider(1%2C%2010%2C%20value%3D4%2C%20label%3D%22width%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bfn_code%2C%20fn_width%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20fn_code%2C%20fn_width%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(fn_code%2C%20fn_width)%3A%0A%20%20%20%20_code%2C%20_width%20%3D%20fn_code.value%2C%20fn_width.value%0A%20%20%20%20_padded%20%3D%20_code.zfill(_width)%0A%20%20%20%20code_view(%0A%20%20%20%20%20%20%20%20%5B(f'pad_code(%22%7B_code%7D%22%2C%20%7B_width%7D)%20%20%20%20%20%20%20%20%20%20%23%20the%20call'%2C%20True)%2C%0A%20%20%20%20%20%20%20%20%20(%22%22%2C%20False)%2C%0A%20%20%20%20%20%20%20%20%20(%22def%20pad_code(code%2C%20width)%3A%22%2C%20True)%2C%0A%20%20%20%20%20%20%20%20%20(f'%20%20%20%20%23%20inside%3A%20code%20%3D%20%22%7B_code%7D%22%2C%20width%20%3D%20%7B_width%7D'%2C%20True)%2C%0A%20%20%20%20%20%20%20%20%20(f'%20%20%20%20padded%20%3D%20code.zfill(width)%20%20%20%23%20padded%20%3D%20%22%7B_padded%7D%22'%2C%20True)%2C%0A%20%20%20%20%20%20%20%20%20(%22%20%20%20%20return%20padded%22%2C%20True)%5D%2C%0A%20%20%20%20%20%20%20%20caption%3Df'The%20call%20%3Ccode%3Epad_code(%22%7Bhtml.escape(_code)%7D%22%2C%20%7B_width%7D)%3C%2Fcode%3E%20gives%20back%20%3Cb%3E%22%7Bhtml.escape(_padded)%7D%22%3C%2Fb%3E.'%0A%20%20%20%20%20%20%20%20%2B%20(%22%20The%20code%20was%20already%20long%20enough%2C%20so%20nothing%20was%20added.%22%20if%20len(_code)%20%3E%3D%20_width%20else%20%22%22)%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.6%20How%20the%20exercises%20work%0A%0A%20%20%20%20Each%20exercise%20has%20**three%20parts**%2C%20one%20under%20the%20other%3A%0A%0A%20%20%20%201.%20**The%20task**%2C%20as%20text%20on%20the%20page%3A%20what%20to%20write%2C%20with%20examples%20of%20inputs%20and%20the%20results%20you%20should%20get.%0A%20%20%20%202.%20**The%20function%20to%20finish**%2C%20in%20a%20code%20cell.%20It%20already%20has%20its%20name%2C%20its%20inputs%2C%20step-by-step%20comments%2C%20and%20one%20line%20you%20replace%3A%0A%0A%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20def%20pad_code(code%2C%20width)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%23%20Use%20the%20text%20method%20.zfill(...)%20on%20%60code%60%2C%20with%20%60width%60.%20Write%3A%20return%20...%0A%20%20%20%20%20%20%20%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.1%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%60raise%20NotImplementedError(...)%60%20means%20*%22not%20written%20yet%22*%3A%20it%20stops%20the%20function%20on%20purpose.%20Write%20your%20code%2C%20**delete%20that%20line**%2C%20and%20run%20the%20cell.%0A%20%20%20%203.%20**The%20check**%2C%20in%20the%20box%20under%20the%20function.%20It%20**runs%20your%20function**%20on%20several%20inputs%20the%20course%20chose%2C%20and%20compares%20every%20result%20with%20what%20a%20correct%20function%20gives%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.mermaid(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20%20%20%20%20A%5BInputs%20chosen%20by%20the%20course%5D%20--%3E%20B%5BYour%20function%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20B%20--%3E%20C%5BYour%20results%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20C%20--%3E%20D%7BSame%20as%20a%20correct%20function%3F%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20D%20--%20every%20one%20--%3E%20E%5BGreen%3A%20correct%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20D%20--%20not%20all%20--%3E%20F%5BRed%3A%20which%20input%2C%20and%20what%20your%20function%20gave%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20G%5BNot%20written%20yet%5D%20--%3E%20H%5BGrey%3A%20not%20attempted%5D%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20check%20never%20shows%20the%20right%20answer%2C%20only%20which%20input%20went%20wrong%20and%20what%20your%20function%20gave%20for%20it.%20That%20is%20on%20purpose%3A%20the%20goal%20is%20a%20function%20that%20works%20for%20**any**%20input%2C%20the%20way%20your%20pipeline%20must%20work%20for%20any%20round.%20Typing%20a%20result%20such%20as%20%60return%20%220128%22%60%20passes%20for%20one%20input%20and%20fails%20for%20the%20others.%0A%0A%20%20%20%20When%20a%20check%20is%20red%2C%20try%20your%20function%20yourself%20in%20a%20new%20cell%20(click%20**%2B**%20under%20any%20cell)%2C%20for%20example%20%60pad_code(%227%22%2C%203)%60%2C%20and%20compare%20with%20the%20examples%20in%20the%20task.%0A%0A%20%20%20%20%23%23%23%20Warm-up%3A%20your%20first%20function%0A%0A%20%20%20%20**Task.**%20Finish%20%60double(n)%60%20so%20that%20it%20**returns**%20the%20number%20%60n%60%20times%20two%20(%60*%60%20multiplies).%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60double(21)%60%20%7C%20%6042%60%20%7C%0A%20%20%20%20%7C%20%60double(0)%60%20%7C%20%600%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Warm-up%3A%20return%20n%20times%20two.%20See%20the%20task%20above.%0Adef%20double(n)%3A%0A%20%20%20%20%23%20Write%20one%20line%20that%20starts%20with%20return%0A%20%20%20%20raise%20NotImplementedError(%22Warm-up%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Warm-up%22%2C%20double%2C%20%5B(f%22double(%7Bn%7D)%22%2C%20(n%2C))%20for%20n%20in%20%5B21%2C%200%2C%20-3%2C%207%2C%201000%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22numbers%22%2C%20hint%3D%22Use%20%60return%60%2C%20not%20%60print%60.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20Python%20refresher%0A%0A%20%20%20%20Eight%20short%20parts%2C%20each%20with%20the%20Python%20you%20will%20need%2C%20an%20explainer%20to%20play%20with%2C%20and%20a%20**Your%20turn**.%0A%0A%20%20%20%20%23%23%23%204.1%20Codes%20are%20text%0A%0A%20%20%20%20%60%220128%22%60%20and%20%60128%60%20are%20different%20things.%20Text%20keeps%20every%20character%3B%20a%20number%20keeps%20only%20the%20value.%20Turning%20a%20code%20into%20a%20number%20throws%20away%20its%20leading%20zero%2C%20and%20you%20cannot%20get%20it%20back%20from%20the%20number%20alone.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20predict_int%20%3D%20mo.ui.radio(options%3D%5B%220128%22%2C%20%22128%22%2C%20%22an%20error%22%5D%2C%20label%3D'**Predict.**%20What%20does%20%60int(%220128%22)%60%20give%3F')%0A%20%20%20%20predict_int%0A%20%20%20%20return%20(predict_int%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(predict_int)%3A%0A%20%20%20%20mo.stop(predict_int.value%20is%20None%2C%20pick_first())%0A%20%20%20%20mo.md(f'You%20predicted%20%60%7Bpredict_int.value%7D%60.%20Python%20gives%20**%60%7Bint(%220128%22)%7D%60**%3A%20the%20zero%20is%20gone.%20Text%20has%20a%20method%20that%20puts%20zeros%20back%20on%20the%20left%2C%20%60.zfill(width)%60%3A%20it%20adds%20zeros%20until%20the%20text%20is%20%60width%60%20characters%20long.%20Try%20it%20below%3B%20the%20added%20zeros%20are%20highlighted.')%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20z_code%20%3D%20mo.ui.text(value%3D%22128%22%2C%20label%3D%22text%3A%22)%0A%20%20%20%20z_width%20%3D%20mo.ui.slider(1%2C%2010%2C%20value%3D4%2C%20label%3D%22width%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bz_code%2C%20z_width%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20z_code%2C%20z_width%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(z_code%2C%20z_width)%3A%0A%20%20%20%20_padded%20%3D%20z_code.value.zfill(z_width.value)%0A%20%20%20%20_added%20%3D%20len(_padded)%20-%20len(z_code.value)%0A%20%20%20%20_shown%20%3D%20%22%3Cspan%20style%3D'background%3Argba(255%2C165%2C0%2C.35)%3Bfont-weight%3A700'%3E%22%20%2B%20%220%22%20*%20_added%20%2B%20%22%3C%2Fspan%3E%22%20%2B%20html.escape(z_code.value)%0A%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20f%22%3Cp%20style%3D'font-family%3Aui-monospace%2CMenlo%2CConsolas%2Cmonospace%3Bfont-size%3A1.1em'%3E%5C%22%7Bhtml.escape(z_code.value)%7D%5C%22.zfill(%7Bz_width.value%7D)%20%E2%86%92%20%5C%22%7B_shown%7D%5C%22%3C%2Fp%3E%22%0A%20%20%20%20%20%20%20%20f%22%3Cp%3E%7B_added%7D%20zero(s)%20added%3B%20the%20result%20has%20%7Blen(_padded)%7D%20characters.%3C%2Fp%3E%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.1%20%C2%B7%20Pad%20a%20code%0A%0A%20%20%20%20**Task.**%20Finish%20%60pad_code(code%2C%20width)%60%3A%20return%20the%20text%20%60code%60%2C%20padded%20on%20the%20left%20with%20zeros%20until%20it%20is%20%60width%60%20characters%20long.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60pad_code(%22128%22%2C%204)%60%20%7C%20%60%220128%22%60%20%7C%0A%20%20%20%20%7C%20%60pad_code(%227%22%2C%203)%60%20%7C%20%60%22007%22%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.1%3A%20pad%20%60code%60%20with%20zeros%20to%20%60width%60%20characters.%20See%20the%20task%20above.%0Adef%20pad_code(code%2C%20width)%3A%0A%20%20%20%20%23%20Use%20the%20text%20method%20.zfill(...)%20on%20%60code%60%2C%20with%20%60width%60.%20Write%3A%20return%20...%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.1%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.1%22%2C%20pad_code%2C%0A%20%20%20%20%20%20%20%20%5B(f%22pad_code(%7Bc!r%7D%2C%20%7Bw%7D)%22%2C%20(c%2C%20w))%20for%20c%2C%20w%20in%20%5B(%22128%22%2C%204)%2C%20(%227%22%2C%203)%2C%20(%2240500%22%2C%2010)%2C%20(%220012%22%2C%204)%2C%20(%2299%22%2C%202)%2C%20(%221%22%2C%206)%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22codes%22%2C%20hint%3D%22Look%20at%20what%20%60.zfill%60%20does%20with%20a%20code%20that%20is%20already%20long%20enough.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.2%20Collections%3A%20lists%2C%20sets%20and%20dictionaries%0A%0A%20%20%20%20-%20A%20**list**%20keeps%20things%20in%20order%20and%20can%20repeat%20them%3A%20%60%5B%22hhid%22%2C%20%22age%22%2C%20%22weight%22%5D%60.%20Square%20brackets.%0A%20%20%20%20-%20A%20**set**%20keeps%20each%20thing%20once%2C%20in%20no%20order%3A%20%60%7B%2201%22%2C%20%2202%22%7D%60.%20Curly%20brackets.%20Sets%20answer%20questions%20about%20membership.%0A%20%20%20%20-%20A%20**dictionary**%20maps%20a%20**key**%20to%20a%20**value**%3A%20%60roles%20%3D%20%7B%22area_code%22%3A%20%22psgc%22%7D%60%20says%20the%20area%20code%20is%20in%20the%20column%20called%20%60psgc%60.%20Look%20a%20value%20up%20with%20%60roles%5B%22area_code%22%5D%60.%0A%0A%20%20%20%20The%20four%20set%20operations%2C%20with%20area%20codes.%20Choose%20which%20codes%20are%20in%20the%20round%20and%20which%20are%20in%20the%20code%20list%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_options%20%3D%20%5B%220100105000%22%2C%20%220100205000%22%2C%20%220100305000%22%2C%20%220200105000%22%2C%20%220600399000%22%5D%0A%20%20%20%20in_round%20%3D%20mo.ui.multiselect(options%3D_options%2C%20value%3D%5B%220100105000%22%2C%20%220100205000%22%2C%20%220600399000%22%5D%2C%20label%3D%22codes%20in%20the%20round%3A%22)%0A%20%20%20%20in_list%20%3D%20mo.ui.multiselect(options%3D_options%2C%20value%3D%5B%220100105000%22%2C%20%220100205000%22%2C%20%220100305000%22%2C%20%220200105000%22%5D%2C%20label%3D%22codes%20in%20the%20code%20list%3A%22)%0A%20%20%20%20mo.hstack(%5Bin_round%2C%20in_list%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20in_list%2C%20in_round%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(in_list%2C%20in_round)%3A%0A%20%20%20%20_a%2C%20_b%20%3D%20set(in_round.value)%2C%20set(in_list.value)%0A%0A%20%20%20%20def%20_s(values)%3A%0A%20%20%20%20%20%20%20%20return%20%22%7B%22%20%2B%20%22%2C%20%22.join(f'%22%7Bv%7D%22'%20for%20v%20in%20sorted(values))%20%2B%20%22%7D%22%20if%20values%20else%20%22set()%22%0A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60%60%60python%0A%20%20%20%20round_codes%20%3D%20%7B_s(_a)%7D%0A%20%20%20%20listed_codes%20%3D%20%7B_s(_b)%7D%0A%0A%20%20%20%20round_codes%20%7C%20listed_codes%20%20%20%23%20in%20either%20(union)%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%3E%20%7B_s(_a%20%7C%20_b)%7D%0A%20%20%20%20round_codes%20%26%20listed_codes%20%20%20%23%20in%20both%20(intersection)%20%20%20%20%20%20%20%20%20-%3E%20%7B_s(_a%20%26%20_b)%7D%0A%20%20%20%20round_codes%20-%20listed_codes%20%20%20%23%20in%20the%20round%2C%20not%20in%20the%20list%20%20-%3E%20%7B_s(_a%20-%20_b)%7D%0A%20%20%20%20listed_codes%20-%20round_codes%20%20%20%23%20in%20the%20list%2C%20not%20in%20the%20round%20%20-%3E%20%7B_s(_b%20-%20_a)%7D%0A%20%20%20%20%60%60%60%0A%20%20%20%20%60round_codes%20-%20listed_codes%60%20are%20the%20codes%20to%20report%20to%20the%20field%20office.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.2%20%C2%B7%20Codes%20that%20are%20not%20on%20the%20list%0A%0A%20%20%20%20**Task.**%20Finish%20%60codes_not_listed(round_codes%2C%20listed_codes)%60%3A%20return%20the%20**set**%20of%20codes%20that%20are%20in%20%60round_codes%60%20but%20not%20in%20%60listed_codes%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60codes_not_listed(%7B%2201%22%2C%20%2202%22%2C%20%2299%22%7D%2C%20%7B%2201%22%2C%20%2202%22%2C%20%2203%22%7D)%60%20%7C%20%60%7B%2299%22%7D%60%20%7C%0A%20%20%20%20%7C%20%60codes_not_listed(%7B%2201%22%7D%2C%20%7B%2201%22%2C%20%2202%22%7D)%60%20%7C%20%60set()%60%20(an%20empty%20set)%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.2%3A%20the%20codes%20in%20round_codes%20that%20are%20not%20in%20listed_codes.%20See%20the%20task%20above.%0Adef%20codes_not_listed(round_codes%2C%20listed_codes)%3A%0A%20%20%20%20%23%20Both%20inputs%20are%20sets.%20One%20operator%20between%20them%20gives%20the%20answer.%20Write%3A%20return%20...%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.2%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.2%22%2C%20codes_not_listed%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20round%20codes%2C%20three%20listed%22%2C%20(%7B%220100105000%22%2C%20%220100205000%22%2C%20%220600399000%22%7D%2C%20%7B%220100105000%22%2C%20%220100205000%22%2C%20%220100305000%22%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20code%20listed%22%2C%20(%7B%2201%22%2C%20%2202%22%7D%2C%20%7B%2201%22%2C%20%2202%22%2C%20%2203%22%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nothing%20listed%22%2C%20(%7B%2201%22%2C%20%2202%22%7D%2C%20set()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20codes%20missing%22%2C%20(%7B%2211%22%2C%20%2212%22%2C%20%2213%22%2C%20%2214%22%7D%2C%20%7B%2212%22%2C%20%2214%22%7D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20sets%22%2C%20hint%3D%22Look%20at%20the%20four%20operations%20in%20the%20explainer%20above.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.3%20Loops%20and%20f-strings%20that%20write%20SQL%0A%0A%20%20%20%20-%20A%20**for%20loop**%20repeats%20its%20indented%20lines%20once%20for%20each%20item%20of%20a%20list%3A%20%60for%20column%20in%20columns%3A%60%20gives%20%60column%60%20each%20value%20in%20turn.%0A%20%20%20%20-%20An%20**f-string**%20(text%20with%20an%20%60f%60%20in%20front)%20puts%20values%20into%20text%3A%20with%20%60table%20%3D%20%22round1%22%60%2C%20%60f%22SELECT%20*%20FROM%20%7Btable%7D%22%60%20gives%20%60SELECT%20*%20FROM%20round1%60.%20The%20names%20in%20%60%7B%20%7D%60%20are%20replaced%20by%20their%20values.%0A%20%20%20%20-%20%60%22%2C%20%22.join(items)%60%20glues%20a%20list%20of%20texts%20into%20one%20text%2C%20with%20%60%22%2C%20%22%60%20between%20them.%0A%0A%20%20%20%20Together%20they%20write%20SQL%20for%20you%2C%20so%20you%20never%20type%20a%20long%20column%20list%20by%20hand.%20Pick%20columns%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20loop_columns%20%3D%20mo.ui.multiselect(options%3D%5B%22hhid%22%2C%20%22member_no%22%2C%20%22area_code%22%2C%20%22age%22%2C%20%22sex%22%2C%20%22weight%22%5D%2C%20value%3D%5B%22hhid%22%2C%20%22area_code%22%2C%20%22weight%22%5D%2C%20label%3D%22columns%3A%22)%0A%20%20%20%20loop_columns%0A%20%20%20%20return%20(loop_columns%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(loop_columns)%3A%0A%20%20%20%20_cols%20%3D%20loop_columns.value%0A%20%20%20%20_loop%20%3D%20%22%5Cn%22.join(f%22keep%20the%20column%20%7Bc%7D%22%20for%20c%20in%20_cols)%20or%20%22(nothing%3A%20the%20list%20is%20empty)%22%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60%60%60python%0A%20%20%20%20columns%20%3D%20%7B_cols!r%7D%0A%20%20%20%20for%20column%20in%20columns%3A%0A%20%20%20%20%20%20%20%20print(f%22keep%20the%20column%20%7B%7Bcolumn%7D%7D%22)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%60%60%60text%0A%20%20%20%20%7B_loop%7D%0A%20%20%20%20%60%60%60%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%22%2C%20%22.join(columns)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20-%3E%20%7B%22%2C%20%22.join(_cols)!r%7D%0A%20%20%20%20f%22SELECT%20%7B%7B'%2C%20'.join(columns)%7D%7D%20FROM%20round1%22%20%20%23%20-%3E%20%7Bf%22SELECT%20%7B'%2C%20'.join(_cols)%7D%20FROM%20round1%22!r%7D%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.3%20%C2%B7%20Write%20a%20SELECT%0A%0A%20%20%20%20**Task.**%20Finish%20%60select_sql(columns%2C%20table)%60%3A%20return%20the%20SQL%20text%20that%20selects%20the%20%60columns%60%20from%20%60table%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60select_sql(%5B%22hhid%22%2C%20%22age%22%5D%2C%20%22round1%22)%60%20%7C%20%60%22SELECT%20hhid%2C%20age%20FROM%20round1%22%60%20%7C%0A%20%20%20%20%7C%20%60select_sql(%5B%22weight%22%5D%2C%20%22people%22)%60%20%7C%20%60%22SELECT%20weight%20FROM%20people%22%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.3%3A%20the%20SQL%20text%20that%20selects%20%60columns%60%20from%20%60table%60.%20See%20the%20task%20above.%0Adef%20select_sql(columns%2C%20table)%3A%0A%20%20%20%20%23%20Step%201%3A%20glue%20the%20columns%20into%20one%20text%20with%20%22%2C%20%22.join(...)%0A%20%20%20%20%23%20Step%202%3A%20put%20that%20text%20and%20%60table%60%20into%20an%20f-string%3A%20f%22SELECT%20...%20FROM%20...%22%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.3%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.3%22%2C%20select_sql%2C%0A%20%20%20%20%20%20%20%20%5B(f%22select_sql(%7Bcols!r%7D%2C%20%7Bt!r%7D)%22%2C%20(cols%2C%20t))%20for%20cols%2C%20t%20in%20%5B(%5B%22hhid%22%2C%20%22area_code%22%2C%20%22weight%22%5D%2C%20%22round1%22)%2C%20(%5B%22age%22%5D%2C%20%22people%22)%2C%20(%5B%22region%22%2C%20%22persons%22%2C%20%22households%22%5D%2C%20%22totals%22)%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22column%20lists%22%2C%20hint%3D%22Check%20the%20spaces%3A%20a%20comma%20and%20one%20space%20between%20columns%2C%20one%20space%20around%20SELECT%20and%20FROM.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.4%20Slices%3A%20parts%20of%20a%20text%0A%0A%20%20%20%20Each%20character%20of%20a%20text%20has%20a%20**position**%2C%20counted%20from%20**0**.%20A%20**slice**%20%60text%5Bstart%3Astop%5D%60%20takes%20the%20characters%20from%20%60start%60%20up%20to%2C%20**but%20not%20including**%2C%20%60stop%60.%20Leave%20out%20%60start%60%20to%20begin%20at%20the%20start%3A%20%60text%5B%3A2%5D%60%20is%20the%20first%20two%20characters.%0A%0A%20%20%20%20An%20area%20code%20is%20made%20of%20parts%3A%20region%20(2%20digits)%2C%20province%20(3)%2C%20municipality%20(2)%2C%20and%203%20zeros.%20Move%20the%20sliders%20to%20slice%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20slice_start%20%3D%20mo.ui.slider(0%2C%2010%2C%20value%3D0%2C%20label%3D%22start%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20slice_stop%20%3D%20mo.ui.slider(0%2C%2010%2C%20value%3D2%2C%20label%3D%22stop%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bslice_start%2C%20slice_stop%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20slice_start%2C%20slice_stop%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(slice_start%2C%20slice_stop)%3A%0A%20%20%20%20_code%20%3D%20%220100305000%22%0A%20%20%20%20_parts%20%3D%20%5B%22region%22%5D%20*%202%20%2B%20%5B%22province%22%5D%20*%203%20%2B%20%5B%22municipality%22%5D%20*%202%20%2B%20%5B%22zeros%22%5D%20*%203%0A%20%20%20%20_cells%20%3D%20%22%22.join(%0A%20%20%20%20%20%20%20%20f%22%3Ctd%20style%3D'padding%3A4px%209px%3Btext-align%3Acenter%3Bborder%3A1px%20solid%20rgba(128%2C128%2C128%2C.4)%3B%22%0A%20%20%20%20%20%20%20%20%2B%20(%22background%3Argba(255%2C165%2C0%2C.35)%3Bfont-weight%3A700%22%20if%20slice_start.value%20%3C%3D%20i%20%3C%20slice_stop.value%20else%20%22opacity%3A.5%22)%0A%20%20%20%20%20%20%20%20%2B%20f%22'%3E%7Bch%7D%3Cdiv%20style%3D'font-size%3A.7em%3Bopacity%3A.7'%3E%7Bi%7D%3C%2Fdiv%3E%3C%2Ftd%3E%22%0A%20%20%20%20%20%20%20%20for%20i%2C%20ch%20in%20enumerate(_code)%0A%20%20%20%20)%0A%20%20%20%20_labels%20%3D%20%22%22.join(f%22%3Ctd%20colspan%3D'%7Bn%7D'%20style%3D'text-align%3Acenter%3Bfont-size%3A.8em%3Bpadding-top%3A4px'%3E%7Bname%7D%3C%2Ftd%3E%22%20for%20name%2C%20n%20in%20%5B(%22region%22%2C%202)%2C%20(%22province%22%2C%203)%2C%20(%22municipality%22%2C%202)%2C%20(%22%22%2C%203)%5D)%0A%20%20%20%20_result%20%3D%20_code%5Bslice_start.value%3Aslice_stop.value%5D%0A%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20f%22%3Ctable%20style%3D'border-collapse%3Acollapse%3Bfont-family%3Aui-monospace%2CMenlo%2CConsolas%2Cmonospace%3Bfont-size%3A1.1em'%3E%3Ctr%3E%7B_cells%7D%3C%2Ftr%3E%3Ctr%3E%7B_labels%7D%3C%2Ftr%3E%3C%2Ftable%3E%22%0A%20%20%20%20%20%20%20%20f%22%3Cp%20style%3D'font-family%3Aui-monospace%2CMenlo%2CConsolas%2Cmonospace'%3E%5C%22%7B_code%7D%5C%22%5B%7Bslice_start.value%7D%3A%7Bslice_stop.value%7D%5D%20%E2%86%92%20%5C%22%7B_result%7D%5C%22%3C%2Fp%3E%22%0A%20%20%20%20%20%20%20%20f%22%3Cp%3EThe%20small%20numbers%20are%20the%20positions.%20%7B'An%20empty%20result%3A%20stop%20is%20not%20after%20start.'%20if%20not%20_result%20else%20'Try%200%3A2%20(region)%2C%200%3A5%20(region%20and%20province)%2C%202%3A5%20(province%20only).'%7D%3C%2Fp%3E%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.4%20%C2%B7%20The%20region%20of%20an%20area%20code%0A%0A%20%20%20%20**Task.**%20Finish%20%60region_of(area_code%2C%20width%3D2)%60%3A%20return%20the%20first%20%60width%60%20characters%20of%20%60area_code%60.%20With%20the%20default%20width%20(2)%2C%20that%20is%20the%20region.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60region_of(%220100305000%22)%60%20%7C%20%60%2201%22%60%20%7C%0A%20%20%20%20%7C%20%60region_of(%220100305000%22%2C%205)%60%20%7C%20%60%2201003%22%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.4%3A%20the%20first%20%60width%60%20characters%20of%20%60area_code%60.%20See%20the%20task%20above.%0Adef%20region_of(area_code%2C%20width%3D2)%3A%0A%20%20%20%20%23%20Use%20a%20slice%3A%20text%5B%3An%5D%20is%20the%20first%20n%20characters.%20Write%3A%20return%20...%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.4%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.4%22%2C%20region_of%2C%0A%20%20%20%20%20%20%20%20%5B(%22region_of('0100305000')%22%2C%20(%220100305000%22%2C))%2C%20(%22region_of('0100305000'%2C%205)%22%2C%20(%220100305000%22%2C%205))%2C%0A%20%20%20%20%20%20%20%20%20(%22region_of('0600399000')%22%2C%20(%220600399000%22%2C))%2C%20(%22region_of('1234'%2C%203)%22%2C%20(%221234%22%2C%203))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22calls%22%2C%20hint%3D%22Use%20the%20input%20%60width%60%20in%20your%20slice%2C%20not%20a%20fixed%20number.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.5%20pandas%3A%20Series%20and%20DataFrames%0A%0A%20%20%20%20**pandas**%20is%20the%20library%20for%20tables.%20A%20**Series**%20is%20one%20column%20of%20values%3B%20a%20**DataFrame**%20is%20a%20table%20made%20of%20Series%2C%20each%20with%20a%20name.%20%60people%5B%22age%22%5D%60%20takes%20the%20column%20%60age%60%20out%20of%20the%20table%20%60people%60.%0A%0A%20%20%20%20Many%20operations%20work%20on%20**every%20value%20at%20once**%20and%20give%20one%20answer%20per%20value%3A%20%60.isna()%60%20gives%20%60True%60%20where%20a%20value%20is%20*missing*%2C%20%60.eq('')%60%20gives%20%60True%60%20where%20it%20is%20the%20empty%20text%2C%20and%20%60a%20%7C%20b%60%20means%20%22a%20or%20b%22%2C%20value%20by%20value.%20%60.sum()%60%20then%20counts%20the%20%60True%60s.%0A%0A%20%20%20%20An%20empty%20text%20%60''%60%20is%20**not**%20missing%20to%20pandas%3A%20it%20is%20a%20text%20with%20nothing%20in%20it.%20Survey%20files%20use%20both%2C%20so%20you%20check%20for%20both.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20area%20%3D%20pd.Series(%5B%220100105000%22%2C%20None%2C%20%22%22%2C%20%220600305000%22%5D)%0A%20%20%20%20people%20%3D%20pd.DataFrame(%7B%22hhid%22%3A%20%5B%22H1%22%2C%20%22H1%22%2C%20%22H2%22%5D%2C%20%22age%22%3A%20%5B34%2C%209%2C%2061%5D%2C%20%22weight%22%3A%20%5B512.5%2C%20498.0%2C%20730.25%5D%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22A%20Series%2C%20%60area%60%2C%20and%20what%20each%20check%20gives%20for%20each%20value%3A%22)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22value%22%3A%20area%2C%20%22area.isna()%22%3A%20area.isna()%2C%20'area.eq(%22%22)'%3A%20area.eq(%22%22)%2C%20%22either%20(%7C)%22%3A%20area.isna()%20%7C%20area.eq(%22%22)%7D)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22A%20DataFrame%2C%20%60people%60%3A%22)%2C%20people%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60people%5B'weight'%5D.sum()%60%20gives%20%7Bpeople%5B'weight'%5D.sum()%3A%2C.2f%7D.%20%60people%5Bpeople%5B'age'%5D%20%3E%3D%2015%5D%60%20keeps%20the%20rows%20where%20the%20condition%20is%20%60True%60%3A%20%7Blen(people%5Bpeople%5B'age'%5D%20%3E%3D%2015%5D)%7D%20rows.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(area%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20predict_isna%20%3D%20mo.ui.radio(options%3D%5B%220%22%2C%20%221%22%2C%20%222%22%5D%2C%20label%3D%22**Predict.**%20What%20does%20%60area.isna().sum()%60%20give%3F%22)%0A%20%20%20%20predict_isna%0A%20%20%20%20return%20(predict_isna%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(area%2C%20predict_isna)%3A%0A%20%20%20%20mo.stop(predict_isna.value%20is%20None%2C%20pick_first())%0A%20%20%20%20mo.md(f%22You%20predicted%20%60%7Bpredict_isna.value%7D%60.%20pandas%20gives%20**%7Bint(area.isna().sum())%7D**%3A%20only%20%60None%60%20counts%20as%20missing.%20Look%20at%20the%20table%20above%3A%20the%20empty%20text%20has%20%60False%60%20under%20%60area.isna()%60.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.5%20%C2%B7%20Missing%20or%20empty%0A%0A%20%20%20%20**Task.**%20Finish%20%60count_missing_or_empty(values)%60%3A%20%60values%60%20is%20a%20Series%3B%20return%20how%20many%20of%20its%20values%20are%20missing%20**or**%20are%20the%20empty%20text%20%60''%60%2C%20as%20a%20whole%20number.%0A%0A%20%20%20%20%7C%20values%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60pd.Series(%5B'01'%2C%20None%2C%20''%2C%20'02'%5D)%60%20%7C%20%602%60%20%7C%0A%20%20%20%20%7C%20%60pd.Series(%5B'01'%2C%20'02'%5D)%60%20%7C%20%600%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.5%3A%20count%20the%20values%20that%20are%20missing%20or%20empty.%20See%20the%20task%20above.%0Adef%20count_missing_or_empty(values)%3A%0A%20%20%20%20%23%20Step%201%3A%20one%20condition%20for%20%22missing%22%2C%20one%20for%20%22is%20the%20empty%20text%22%20(see%20the%20table%20above)%0A%20%20%20%20%23%20Step%202%3A%20combine%20them%20with%20%7C%20(%22or%22)%2C%20then%20count%20the%20True%20values%20with%20.sum()%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20count%20as%20a%20whole%20number%20with%20int(...)%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.5%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.5%22%2C%20count_missing_or_empty%2C%0A%20%20%20%20%20%20%20%20%5B(f%22a%20Series%20of%20%7Bv!r%7D%22%2C%20(pd.Series(v)%2C))%20for%20v%20in%20%5B%5B%220100105000%22%2C%20None%2C%20%22%22%2C%20%220600305000%22%5D%2C%20%5B%2201%22%2C%20%2202%22%5D%2C%20%5BNone%2C%20None%2C%20%22%22%5D%2C%20%5B%22%22%2C%20%22%22%2C%20%22%22%2C%20%2203%22%5D%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22Series%22%2C%20hint%3D%22Count%20both%20kinds%3A%20missing%20values%20and%20empty%20texts.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.6%20Classes%3A%20the%20shape%20of%20a%20record%0A%0A%20%20%20%20A%20**class**%20describes%20what%20one%20record%20must%20look%20like.%20With%20**pydantic**%2C%20the%20class%20also%20**checks**%20each%20record%20when%20it%20is%20made%2C%20and%20refuses%20one%20that%20does%20not%20fit.%20Below%2C%20a%20household%20must%20have%20a%20text%20%60hhid%60%20and%20a%20whole-number%20%60size%60%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20class%20Household(BaseModel)%3A%0A%20%20%20%20%20%20%20%20hhid%3A%20str%0A%20%20%20%20%20%20%20%20size%3A%20int%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20You%20make%20a%20record%20by%20calling%20the%20class%20with%20**named**%20values%3A%20%60Household(hhid%3D%22H00000001%22%2C%20size%3D3)%60.%20Try%20some%20values%20(a%20size%20of%20%603%60%2C%20%60%223%22%60%2C%20%60three%60%2C%20%602.5%60)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20Household(BaseModel)%3A%0A%20%20%20%20hhid%3A%20str%0A%20%20%20%20size%3A%20int%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20hh_id%20%3D%20mo.ui.text(value%3D%22H00000001%22%2C%20label%3D%22hhid%3A%22)%0A%20%20%20%20hh_size%20%3D%20mo.ui.text(value%3D%22three%22%2C%20label%3D%22size%3A%22)%0A%20%20%20%20mo.hstack(%5Bhh_id%2C%20hh_size%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20hh_id%2C%20hh_size%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(hh_id%2C%20hh_size)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_made%20%3D%20Household(hhid%3Dhh_id.value%2C%20size%3Dhh_size.value)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**Accepted%3A**%20%60%7B_made!r%7D%60.%20pydantic%20turned%20the%20text%20%60%7Bhh_size.value!r%7D%60%20into%20the%20number%20%60%7B_made.size%7D%60%2C%20because%20it%20means%20a%20whole%20number%20without%20doubt.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20except%20ValidationError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**Refused.**%20pydantic%20says%3A%5Cn%5Cn%60%60%60%5Cn%7B_error%7D%5Cn%60%60%60%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.6%20%C2%B7%20Make%20a%20household%0A%0A%20%20%20%20**Task.**%20Finish%20%60make_household(hhid%2C%20size)%60%3A%20return%20a%20%60Household%60%20with%20the%20given%20%60hhid%60%20and%20%60size%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60make_household(%22H00000042%22%2C%204)%60%20%7C%20%60Household(hhid%3D%22H00000042%22%2C%20size%3D4)%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.6%3A%20return%20a%20Household%20with%20the%20given%20hhid%20and%20size.%20See%20the%20task%20above.%0Adef%20make_household(hhid%2C%20size)%3A%0A%20%20%20%20%23%20Call%20the%20class%20with%20named%20values%2C%20like%20Household(hhid%3D...%2C%20size%3D...).%20Write%3A%20return%20...%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.6%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.6%22%2C%20make_household%2C%0A%20%20%20%20%20%20%20%20%5B(%22make_household('H00000042'%2C%204)%22%2C%20(%22H00000042%22%2C%204))%2C%20(%22make_household('H00000001'%2C%20'3')%22%2C%20(%22H00000001%22%2C%20%223%22))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22households%22%2C%20summarize%3Dlambda%20h%3A%20(type(h).__name__%2C%20h.hhid%2C%20h.size)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Return%20a%20Household%2C%20not%20a%20dictionary%20or%20a%20tuple.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.7%20Errors%20you%20expect%3A%20%60try%60%20and%20%60except%60%0A%0A%20%20%20%20Some%20failures%20are%20normal%3A%20a%20published%20table%20says%20%60..%60%20where%20a%20number%20would%20be%2C%20and%20%60int(%22..%22)%60%20raises%20a%20**%60ValueError%60**%2C%20which%20would%20stop%20your%20program.%20%60try%60%20%2F%20%60except%60%20says%20what%20to%20do%20instead%3A%0A%0A%20%20%20%20-%20Python%20runs%20the%20lines%20under%20%60try%60%3B%0A%20%20%20%20-%20if%20one%20of%20them%20raises%20the%20error%20named%20after%20%60except%60%2C%20Python%20jumps%20to%20the%20lines%20under%20%60except%60%2C%20and%20carries%20on%3B%0A%20%20%20%20-%20if%20nothing%20goes%20wrong%2C%20the%20%60except%60%20lines%20are%20skipped.%0A%0A%20%20%20%20Catch%20only%20the%20**specific**%20error%20you%20expect.%20Never%20write%20a%20bare%20%60except%3A%60%3A%20it%20would%20also%20hide%20errors%20you%20did%20not%20expect%2C%20such%20as%20a%20typo%20in%20your%20own%20code.%20Type%20a%20text%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20try_box%20%3D%20mo.ui.text(value%3D%22..%22%2C%20label%3D%22text%3A%22)%0A%20%20%20%20try_box%0A%20%20%20%20return%20(try_box%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(try_box)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_number%20%3D%20int(try_box.value)%0A%20%20%20%20%20%20%20%20_failed%2C%20_note%20%3D%20False%2C%20f%22%3Ccode%3Eint(%5C%22%7Bhtml.escape(try_box.value)%7D%5C%22)%3C%2Fcode%3E%20worked%2C%20so%20the%20%3Ccode%3Eexcept%3C%2Fcode%3E%20lines%20were%20skipped%3A%20the%20result%20is%20%3Cb%3E%7B_number%7D%3C%2Fb%3E.%22%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20_failed%2C%20_note%20%3D%20True%2C%20f%22%3Ccode%3Eint(%5C%22%7Bhtml.escape(try_box.value)%7D%5C%22)%3C%2Fcode%3E%20raised%20a%20%3Ccode%3EValueError%3C%2Fcode%3E%2C%20so%20Python%20jumped%20to%20%3Ccode%3Eexcept%3C%2Fcode%3E%3A%20the%20result%20is%20%3Cb%3ENone%3C%2Fb%3E.%22%0A%20%20%20%20code_view(%0A%20%20%20%20%20%20%20%20%5B(f'text%20%3D%20%22%7Btry_box.value%7D%22'%2C%20True)%2C%20(%22try%3A%22%2C%20True)%2C%20(%22%20%20%20%20number%20%3D%20int(text)%22%2C%20not%20_failed)%2C%0A%20%20%20%20%20%20%20%20%20(%22except%20ValueError%3A%22%2C%20_failed)%2C%20(%22%20%20%20%20number%20%3D%20None%22%2C%20_failed)%5D%2C%0A%20%20%20%20%20%20%20%20caption%3D_note%20%2B%20%22%20Try%20%3Ccode%3E42%3C%2Fcode%3E%2C%20%3Ccode%3E%2015%20%3C%2Fcode%3E%2C%20%3Ccode%3E4%2C2%3C%2Fcode%3E%20and%20%3Ccode%3E-%3C%2Fcode%3E.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.7%20%C2%B7%20A%20number%2C%20or%20nothing%0A%0A%20%20%20%20**Task.**%20Finish%20%60to_number(text)%60%3A%20return%20%60text%60%20as%20a%20whole%20number%2C%20or%20%60None%60%20when%20it%20is%20not%20one.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60to_number(%2242%22)%60%20%7C%20%6042%60%20%7C%0A%20%20%20%20%7C%20%60to_number(%22..%22)%60%20%7C%20%60None%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.7%3A%20the%20text%20as%20a%20whole%20number%2C%20or%20None.%20See%20the%20task%20above.%0Adef%20to_number(text)%3A%0A%20%20%20%20%23%20Use%20try%3A%20...%20except%20ValueError%3A%20...%2C%20as%20in%20the%20explainer%20above%2C%20with%20a%20return%20in%20each%20part.%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.7%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.7%22%2C%20to_number%2C%0A%20%20%20%20%20%20%20%20%5B(f%22to_number(%7Bt!r%7D)%22%2C%20(t%2C))%20for%20t%20in%20%5B%2242%22%2C%20%224%2C2%22%2C%20%22..%22%2C%20%22-%22%2C%20%22007%22%2C%20%22%2015%20%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22texts%22%2C%20hint%3D%22Catch%20only%20ValueError.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.8%20Paths%20and%20imports%0A%0A%20%20%20%20%60import%60%20brings%20in%20code%20from%20elsewhere%3A%20a%20library%20(%60import%20pandas%20as%20pd%60%20makes%20pandas%20available%20under%20the%20short%20name%20%60pd%60)%20or%20a%20file%20of%20your%20own%20(the%20notebooks%20import%20%60course.py%60).%0A%0A%20%20%20%20A%20**path**%20says%20where%20a%20file%20is.%20Windows%20writes%20paths%20with%20%60%5C%60%2C%20Mac%20and%20Linux%20with%20%60%2F%60.%20Python's%20paths%20are%20built%20with%20%60%2F%60%20and%20work%20on%20every%20computer%3A%20%60PATHS.rounds%20%2F%20%222023Q4%22%20%2F%20%22survey.dta%22%60.%20%60PATHS%60%2C%20from%20%60course.py%60%2C%20knows%20where%20the%20course%20folders%20are%20on%20**your**%20computer.%20Pick%20a%20round%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20path_round%20%3D%20mo.ui.dropdown(options%3D%5B%222023Q4%22%2C%20%222024Q1%22%2C%20%222024Q2%22%5D%2C%20value%3D%222023Q4%22%2C%20label%3D%22round%3A%22)%0A%20%20%20%20path_round%0A%20%20%20%20return%20(path_round%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(path_round)%3A%0A%20%20%20%20_path%20%3D%20PATHS.rounds%20%2F%20path_round.value%20%2F%20%22survey.dta%22%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60%60%60python%0A%20%20%20%20PATHS.rounds%20%2F%20%22%7Bpath_round.value%7D%22%20%2F%20%22survey.dta%22%0A%20%20%20%20%60%60%60%0A%20%20%20%20-%20on%20this%20computer%3A%20%60%7B_path%7D%60%0A%20%20%20%20-%20written%20the%20Windows%20way%3A%20%60%7BPureWindowsPath(_path)%7D%60%0A%20%20%20%20-%20the%20file%20exists%3A%20**%7B%22yes%22%20if%20_path.exists()%20else%20%22not%20yet%20(it%20arrives%20later%20in%20the%20course)%22%7D**%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Your%20turn%204.8%20%C2%B7%20Where%20a%20round's%20file%20is%0A%0A%20%20%20%20**Task.**%20Finish%20%60round_file(round_id)%60%3A%20return%20the%20path%20to%20a%20round's%20survey%20file%2C%20which%20is%20the%20file%20%60survey.dta%60%20in%20the%20folder%20named%20%60round_id%60%2C%20inside%20%60PATHS.rounds%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20(as%20a%20path%20inside%20the%20course%20folder)%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60round_file(%222023Q4%22)%60%20%7C%20%60rounds%2F2023Q4%2Fsurvey.dta%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0A%23%20Your%20turn%204.8%3A%20the%20path%20to%20a%20round's%20survey.dta.%20See%20the%20task%20above.%0Adef%20round_file(round_id)%3A%0A%20%20%20%20%23%20Start%20from%20PATHS.rounds%20and%20join%20two%20more%20parts%20with%20%2F.%20Write%3A%20return%20...%0A%20%20%20%20raise%20NotImplementedError(%22Your%20turn%204.8%22)%20%20%23%20delete%20this%20line%20when%20your%20code%20is%20ready%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Your%20turn%204.8%22%2C%20round_file%2C%0A%20%20%20%20%20%20%20%20%5B(f%22round_file(%7Br!r%7D)%22%2C%20(r%2C))%20for%20r%20in%20%5B%222023Q4%22%2C%20%222024Q1%22%2C%20%222024Q2%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22rounds%22%2C%20summarize%3Dlambda%20p%3A%20p.relative_to(PATHS.root).as_posix()%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20result%20should%20be%20a%20path%20inside%20the%20course%20folder%2C%20ending%20in%20survey.dta.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20You%20are%20ready%0A%0A%20%20%20%20Open%20the%20next%20notebook%2C%20**%6010_get_round.py%60**%3A%20Round%201%20has%20arrived.%20In%20marimo%2C%20use%20the%20menu%20at%20the%20top%20left%20to%20open%20another%20notebook%2C%20or%20go%20back%20to%20the%20file%20list%20in%20your%20browser%20tab%20and%20click%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%221.1%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
cf63dcd4964292fcdcec111031c23e53