Spaces:
Running
Running
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<link rel="icon" href="./favicon.ico" /> | |
<!-- Preload is necessary because we show these images when we disconnect from the server, | |
but at that point we cannot load these images from the server --> | |
<link rel="preload" href="./assets/gradient-yHQUC_QB.png" as="image" /> | |
<link rel="preload" href="./assets/noise-60BoTA8O.png" as="image" /> | |
<!-- Preload the fonts --> | |
<link rel="preload" href="./assets/Lora-VariableFont_wght-B2ootaw-.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/PTSans-Regular-CxL0S8W7.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/PTSans-Bold-D9fedIX3.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Regular-BTCkDNvf.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Medium-DU3aDxX5.ttf" as="font" crossorigin="anonymous" /> | |
<link rel="preload" href="./assets/FiraMono-Bold-CLVRCuM9.ttf" as="font" crossorigin="anonymous" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<meta name="theme-color" content="#000000" /> | |
<meta name="description" content="a marimo app" /> | |
<link rel="apple-touch-icon" href="./apple-touch-icon.png" /> | |
<link rel="manifest" href="./manifest.json" /> | |
<script data-marimo="true"> | |
function __resizeIframe(obj) { | |
var scrollbarHeight = 20; // Max between windows, mac, and linux | |
function setHeight() { | |
var element = obj.contentWindow.document.documentElement; | |
// If there is no vertical scrollbar, we don't need to resize the iframe | |
if (element.scrollHeight === element.clientHeight) { | |
return; | |
} | |
// Create a new height that includes the scrollbar height if it's visible | |
var hasHorizontalScrollbar = element.scrollWidth > element.clientWidth; | |
var newHeight = element.scrollHeight + (hasHorizontalScrollbar ? scrollbarHeight : 0); | |
// Only update the height if it's different from the current height | |
if (obj.style.height !== `${newHeight}px`) { | |
obj.style.height = `${newHeight}px`; | |
} | |
} | |
// Resize the iframe to the height of the content and bottom scrollbar height | |
setHeight(); | |
// Resize the iframe when the content changes | |
const resizeObserver = new ResizeObserver((entries) => { | |
setHeight(); | |
}); | |
resizeObserver.observe(obj.contentWindow.document.body); | |
} | |
</script> | |
<marimo-filename hidden>notebook.py</marimo-filename> | |
<marimo-mode data-mode='edit' hidden></marimo-mode> | |
<marimo-version data-version='0.11.9' hidden></marimo-version> | |
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"default_width": "medium", "dataframes": "rich", "code_editor_font_size": 14, "theme": "light", "cell_output": "above"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default", "overrides": {}}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "watcher_on_save": "lazy", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000}, "save": {"autosave": "off", "autosave_delay": 1000, "format_on_save": false}, "package_management": {"manager": "pip"}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{}' hidden></marimo-user-config> | |
<marimo-app-config data-config='{"width": "compact"}' hidden></marimo-app-config> | |
<marimo-server-token data-token='123' hidden></marimo-server-token> | |
<title>005 loops</title> | |
<script type="module" crossorigin src="./assets/index-BiV-b1K2.js"></script> | |
<link rel="stylesheet" crossorigin href="./assets/index-DkqMrX_B.css"> | |
<marimo-wasm hidden=""></marimo-wasm> | |
<script> | |
if (window.location.protocol === 'file:') { | |
alert('Warning: This file must be served by an HTTP server to function correctly.'); | |
} | |
</script> | |
<style> | |
#save-button { | |
display: none ; | |
} | |
#filename-input { | |
display: none ; | |
} | |
</style> | |
<marimo-code hidden="" data-show-code="false">import%20marimo%0A%0A__generated_with%20%3D%20%220.11.9%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20%F0%9F%94%84%20Loops%0A%0A%20%20%20%20%20%20%20%20Let's%20learn%20how%20Python%20helps%20us%20repeat%20tasks%20efficiently%20with%20loops.%0A%0A%20%20%20%20%20%20%20%20A%20%22loop%22%20is%20a%20way%20to%20execute%20a%20block%20of%20code%20multiple%20times.%20Python%20has%20two%20%0A%20%20%20%20%20%20%20%20main%20types%20of%20loops%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%23%20For%20loop%3A%20when%20you%20know%20how%20many%20times%20to%20repeat%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(5)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(i)%0A%0A%20%20%20%20%20%20%20%20%23%20While%20loop%3A%20when%20you%20don't%20know%20how%20many%20repetitions%0A%20%20%20%20%20%20%20%20while%20condition%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20do_something()%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20Let's%20start%20with%20a%20simple%20list%20to%20explore%20loops.%20Feel%20free%20to%20modify%20this%20list%20and%20see%20how%20the%20subsequent%20outputs%20change.%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%0Adef%20_()%3A%0A%20%20%20%20sample_fruits%20%3D%20%5B%22apple%22%2C%20%22banana%22%2C%20%22orange%22%2C%20%22grape%22%5D%0A%20%20%20%20return%20(sample_fruits%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20The%20for%20loop%0A%0A%20%20%20%20%20%20%20%20The%20for%20loop%20is%20perfect%20for%20iterating%20over%20sequences.%0A%20%20%20%20%20%20%20%20Try%20changing%20the%20%60sample_fruits%60%20list%20above%20and%20see%20how%20the%20output%20changes.%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%0Adef%20_(sample_fruits)%3A%0A%20%20%20%20for%20_fruit%20in%20sample_fruits%3A%0A%20%20%20%20%20%20%20%20print(f%22I%20like%20%7B_fruit%7Ds!%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Getting%20the%20position%20of%20an%20item%0A%0A%20%20%20%20%20%20%20%20When%20you%20need%20both%20the%20item%20and%20its%20position%2C%20use%20%60enumerate()%60%3A%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%0Adef%20_(sample_fruits)%3A%0A%20%20%20%20for%20_idx%2C%20_fruit%20in%20enumerate(sample_fruits)%3A%0A%20%20%20%20%20%20%20%20print(f%22%7B_idx%20%2B%201%7D.%20%7B_fruit%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Iterating%20over%20a%20range%20of%20numbers%0A%0A%20%20%20%20%20%20%20%20%60range()%60%20is%20a%20powerful%20function%20for%20generating%20sequences%20of%20numbers%3A%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%0Adef%20_()%3A%0A%20%20%20%20print(%22range(5)%3A%22%2C%20list(range(5)))%0A%20%20%20%20print(%22range(2%2C%205)%3A%22%2C%20list(range(2%2C%205)))%0A%20%20%20%20print(%22range(0%2C%2010%2C%202)%3A%22%2C%20list(range(0%2C%2010%2C%202)))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20for%20_i%20in%20range(5)%3A%0A%20%20%20%20%20%20%20%20print(_i)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20The%20%60while%60%20loop%0A%0A%20%20%20%20%20%20%20%20While%20loops%20continue%20as%20long%20as%20a%20condition%20is%20%60True%60.%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%0Adef%20_()%3A%0A%20%20%20%20_count%20%3D%200%0A%20%20%20%20while%20_count%20%3C%205%3A%0A%20%20%20%20%20%20%20%20print(f%22The%20count%20is%20%7B_count%7D%22)%0A%20%20%20%20%20%20%20%20_count%20%2B%3D%201%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Controlling%20loop%20execution%0A%0A%20%20%20%20%20%20%20%20Python%20provides%20several%20ways%20to%20control%20loop%20execution%3A%0A%0A%20%20%20%20%20%20%20%20-%20%60break%60%3A%20exit%20the%20loop%20immediately%0A%0A%20%20%20%20%20%20%20%20-%20%60continue%60%3A%20skip%20to%20the%20next%20iteration%0A%0A%20%20%20%20%20%20%20%20These%20can%20be%20used%20with%20both%20%60for%60%20and%20%60while%60%20loops.%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%0Adef%20_()%3A%0A%20%20%20%20for%20_i%20in%20range(1%2C%206)%3A%0A%20%20%20%20%20%20%20%20if%20_i%20%3D%3D%204%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Breaking%20out%20of%20the%20loop.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20print(_i)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20for%20_i%20in%20range(1%2C%206)%3A%0A%20%20%20%20%20%20%20%20if%20_i%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20print(_i)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Practical%20loop%20patterns%0A%0A%20%20%20%20%20%20%20%20Here%20are%20some%20common%20patterns%20you'll%20use%20with%20loops%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%23%20Pattern%201%3A%20Accumulator%0A%20%20%20%20%20%20%20%20value%20%3D%200%0A%20%20%20%20%20%20%20%20for%20num%20in%20%5B1%2C%202%2C%203%2C%204%2C%205%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20value%20%2B%3D%20num%0A%0A%20%20%20%20%20%20%20%20%23%20Pattern%202%3A%20Search%0A%20%20%20%20%20%20%20%20found%20%3D%20False%0A%20%20%20%20%20%20%20%20for%20item%20in%20items%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20condition%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20found%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20%20%20%20%20%23%20Pattern%203%3A%20Filter%0A%20%20%20%20%20%20%20%20filtered%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20item%20in%20items%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20condition%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20filtered.append(item)%0A%20%20%20%20%20%20%20%20%60%60%60%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_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Next%20steps%0A%0A%20%20%20%20%20%20%20%20Check%20out%20the%20official%20%5BPython%20docs%20on%20loops%20and%20control%20flow%5D(https%3A%2F%2Fdocs.python.org%2F3%2Ftutorial%2Fcontrolflow.html).%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%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A</marimo-code></head> | |
<body> | |
<div id="root"></div> | |
</body> | |
</html> | |