import%20marimo%0A%0A__generated_with%20%3D%20%220.23.13%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Computational%20Notebooks%0A%0A%20%20%20%20Computational%20notebooks%20provide%20an%20interactive%20environment%20for%20combining%20code%2C%20its%20results%2C%20and%20explanatory%20text.%20They%20are%20well%20suited%20to%20iterative%20tasks%20such%20as%20data%20analysis%2C%20because%20they%20let%20you%20test%20and%20document%20your%20work%20step%20by%20step.%20Immediate%20feedback%20and%20ease%20of%20experimentation%20make%20notebooks%20an%20accessible%20way%20to%20learn%20Python.%0A%0A%20%20%20%20%3E%20**Note%3A**%0A%20%20%20%20%3E%20%23%23%20Learning%20Objectives%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20-%20Understand%20what%20computational%20notebooks%20are%20and%20why%20they%20are%20useful%20for%20data%20analysis%0A%20%20%20%20%3E%20-%20Open%20and%20run%20a%20marimo%20notebook%0A%20%20%20%20%3E%20-%20Work%20with%20code%20and%20markdown%20cells%0A%20%20%20%20%3E%20-%20Understand%20how%20marimo%20tracks%20variables%20and%20keeps%20cells%20up%20to%20date%0A%0A%20%20%20%20%23%23%20marimo%0A%0A%20%20%20%20These%20lessons%20use%20%5Bmarimo%5D(https%3A%2F%2Fmarimo.io)%20notebooks.%20A%20marimo%20notebook%20is%20a%20plain%20Python%20file%20(this%20lesson%20is%20the%20file%20%601_notebooks.py%60)%2C%20which%20means%20it%20works%20cleanly%20with%20version%20control%20and%20can%20also%20be%20run%20as%20an%20ordinary%20Python%20script.%0A%0A%20%20%20%20To%20open%20a%20notebook%20for%20editing%2C%20run%20the%20following%20from%20the%20repository%20root%3A%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20uv%20run%20marimo%20edit%20examples%2Fclass%2F1_notebooks.py%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20This%20opens%20the%20notebook%20in%20your%20web%20browser%2C%20where%20you%20can%20run%20cells%2C%20edit%20code%2C%20and%20see%20results.%0A%0A%20%20%20%20%23%23%20Cells%0A%0A%20%20%20%20A%20notebook%20is%20made%20up%20of%20cells%2C%20of%20two%20kinds%3A%0A%0A%20%20%20%20-%20**Code%20cells**%20contain%20Python%20code.%20Outputs%20appear%20with%20the%20cell.%0A%20%20%20%20-%20**Markdown%20cells**%20contain%20formatted%20text%20for%20documentation%20and%20context.%0A%0A%20%20%20%20%23%23%20Running%20Code%0A%0A%20%20%20%20To%20run%20a%20code%20cell%2C%20select%20it%20and%20press%20%60Ctrl%2BEnter%60%20(or%20%60Cmd%2BEnter%60%20on%20Mac)%2C%20or%20click%20its%20play%20button.%20Here's%20an%20example%3A%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%202%20%2B%202%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20output%20appears%20with%20the%20cell.%0A%0A%20%20%20%20Another%20example%3A%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%20x%20%3D%205%0A%20%20%20%20print(x)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Implicit%20Output%0A%0A%20%20%20%20A%20handy%20feature%20of%20notebooks%20is%20that%20the%20value%20of%20the%20last%20expression%20in%20a%20code%20cell%20is%20automatically%20displayed%20as%20output%3A%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%20The%20value%20of%20'answer'%20will%20be%20displayed%0A%20%20%20%20answer%20%3D%2042%0A%20%20%20%20answer%0A%20%20%20%20return%20(answer%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20display%20other%20values%2C%20or%20values%20that%20aren't%20on%20the%20last%20line%20of%20a%20cell%2C%20use%20the%20%60print()%60%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(answer)%3A%0A%20%20%20%20%23%20Explicitly%20displaying%20values%0A%20%20%20%20print(answer)%20%20%23%20This%20will%20show%20the%20value%20of%20'answer'%0A%20%20%20%20doubled%20%3D%20answer%20*%202%0A%20%20%20%20doubled%20%20%23%20Automatically%20displayed%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20marimo%20Tracks%20Variables%0A%0A%20%20%20%20A%20variable%20defined%20in%20one%20cell%20can%20be%20used%20in%20other%20cells.%20In%20the%20next%20cell%20we%20define%20%60city_name%60%3A%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%20Define%20a%20variable%0A%20%20%20%20city_name%20%3D%20%22Seville%22%0A%20%20%20%20return%20(city_name%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Other%20cells%20can%20then%20use%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(city_name)%3A%0A%20%20%20%20print(%22The%20selected%20city%20is%3A%22%2C%20city_name)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Behind%20the%20scenes%2C%20marimo%20reads%20each%20cell%20to%20see%20which%20variables%20it%20defines%20and%20which%20it%20uses%2C%20and%20connects%20the%20cells%20into%20a%20*dataflow%20graph*.%20This%20has%20two%20practical%20consequences%20that%20distinguish%20marimo%20from%20traditional%20notebooks%20(such%20as%20Jupyter)%3A%0A%0A%20%20%20%201.%20**Cells%20re-run%20automatically.**%20If%20you%20change%20%60city_name%60%20to%20%60%22Barcelona%22%60%20in%20its%20defining%20cell%20and%20run%20it%2C%20every%20cell%20that%20uses%20%60city_name%60%20re-runs%20with%20the%20new%20value.%20You%20never%20end%20up%20with%20stale%20results%20from%20cells%20that%20were%20run%20in%20the%20wrong%20order%2C%20which%20is%20a%20common%20source%20of%20confusion%20in%20traditional%20notebooks.%0A%20%20%20%202.%20**A%20variable%20can%20be%20defined%20in%20only%20one%20cell.**%20If%20two%20cells%20both%20assigned%20to%20%60city_name%60%2C%20marimo%20would%20not%20know%20which%20value%20the%20other%20cells%20should%20use%2C%20so%20it%20reports%20an%20error.%20To%20fix%20such%20an%20error%2C%20either%20use%20a%20different%20variable%20name%20or%20combine%20the%20two%20definitions%20into%20one%20cell.%0A%0A%20%20%20%20Because%20of%20these%20rules%2C%20the%20position%20of%20a%20cell%20on%20the%20page%20does%20not%20determine%20when%20it%20runs%3B%20what%20matters%20is%20which%20variables%20connect%20it%20to%20other%20cells.%20Notebooks%20are%20nevertheless%20easier%20to%20read%20when%20they%20flow%20from%20top%20to%20bottom%2C%20so%20these%20lessons%20are%20arranged%20that%20way.%0A%0A%20%20%20%20A%20small%20dependency%20chain%20as%20an%20example%3A%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%20base%20%3D%2010%0A%20%20%20%20base%0A%20%20%20%20return%20(base%2C)%0A%0A%0A%40app.cell%0Adef%20_(base)%3A%0A%20%20%20%20tripled%20%3D%20base%20*%203%0A%20%20%20%20tripled%0A%20%20%20%20return%20(tripled%2C)%0A%0A%0A%40app.cell%0Adef%20_(tripled)%3A%0A%20%20%20%20total%20%3D%20tripled%20%2B%205%0A%20%20%20%20total%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Try%20changing%20%60base%60%20to%20a%20different%20number%20and%20running%20that%20cell%3A%20the%20%60tripled%60%20and%20%60total%60%20cells%20update%20automatically.%0A%0A%20%20%20%20%3E%20**Note%3A**%0A%20%20%20%20%3E%20Variables%20whose%20names%20start%20with%20an%20underscore%20(e.g.%20%60_result%60)%20are%20*local*%20to%20their%20cell.%20They%20can%20be%20reused%20in%20other%20cells%20without%20conflict%2C%20which%20is%20useful%20for%20throwaway%20values%20such%20as%20plot%20axes.%0A%0A%20%20%20%20%23%23%20Adding%20Text%0A%0A%20%20%20%20Markdown%20cells%20use%20%5BMarkdown%5D(https%3A%2F%2Fwww.markdownguide.org%2Fbasic-syntax%2F)%2C%20a%20lightweight%20markup%20language%20that%20uses%20simple%20characters%20for%20formatting%3A%20headings%2C%20bold%20or%20italic%20text%2C%20lists%2C%20and%20tables.%20In%20the%20marimo%20editor%2C%20create%20a%20markdown%20cell%20by%20adding%20a%20new%20cell%20and%20switching%20it%20to%20markdown%20mode%20(or%20start%20typing%20markdown%20and%20marimo%20will%20offer%20to%20convert%20it).%0A%0A%20%20%20%20%23%23%20Tips%0A%0A%20%20%20%20-%20**Troubleshooting%20errors%3A**%20Read%20the%20error%20message%20shown%20with%20the%20cell%3B%20marimo%20also%20highlights%20problems%20such%20as%20a%20variable%20defined%20in%20more%20than%20one%20cell.%0A%20%20%20%20-%20**Saving%3A**%20Saving%20the%20notebook%20simply%20saves%20the%20%60.py%60%20file.%20Because%20it%20is%20plain%20Python%2C%20it%20diffs%20cleanly%20in%20git%20and%20can%20be%20shared%20like%20any%20other%20file.%0A%20%20%20%20-%20**Running%20as%20a%20script%3A**%20A%20marimo%20notebook%20can%20also%20be%20executed%20directly%2C%20e.g.%20%60uv%20run%20python%20examples%2Fclass%2F1_notebooks.py%60.%0A%20%20%20%20-%20**Getting%20help%3A**%20To%20see%20what%20a%20function%20does%2C%20run%20%60help(function_name)%60%20in%20a%20code%20cell.%0A%0A%20%20%20%20%23%23%23%20Exercises%0A%0A%20%20%20%20-%20Create%20a%20new%20code%20cell%2C%20assign%20the%20value%20%60100%60%20to%20a%20variable%20called%20%60distance%60%2C%20and%20print%20it.%0A%20%20%20%20-%20Create%20a%20markdown%20cell%20with%20a%20heading%20and%20a%20short%20description%20of%20what%20the%20notebook%20is%20about.%0A%20%20%20%20-%20Change%20the%20value%20of%20%60base%60%20above%20and%20observe%20which%20cells%20re-run.%0A%20%20%20%20-%20Try%20defining%20%60city_name%60%20a%20second%20time%20in%20a%20new%20cell.%20What%20does%20marimo%20report%3F%20Delete%20the%20cell%20afterwards.%0A%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20-%20Computational%20notebooks%20combine%20code%2C%20output%2C%20and%20explanatory%20text%20in%20an%20interactive%20document.%0A%20%20%20%20-%20A%20marimo%20notebook%20is%20a%20plain%20Python%20file%3B%20open%20it%20with%20%60uv%20run%20marimo%20edit%20%3Cfile%3E%60.%0A%20%20%20%20-%20Notebooks%20contain%20code%20cells%20(Python)%20and%20markdown%20cells%20(documentation).%0A%20%20%20%20-%20marimo%20connects%20cells%20into%20a%20dataflow%20graph%3A%20a%20variable%20may%20be%20defined%20in%20only%20one%20cell%2C%20and%20dependent%20cells%20re-run%20automatically%20when%20their%20inputs%20change.%0A%20%20%20%20-%20Underscore-prefixed%20variables%20are%20local%20to%20a%20cell.%0A%0A%20%20%20%20Next%3A%20%5BPython%20Basics%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fstart%2F2-basics)%20covers%20the%20fundamentals%20of%20the%20Python%20language.%0A%20%20%20%20%22%22%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
7c17b522438c1687ff47d24c98623a89