marimo-learn / _site /python /003_collections.html
Haleshot's picture
Update course content and HTML configurations across various sections, including functional programming, optimization, and probability. Adjust descriptions, enhance user configurations, and ensure consistent theming for improved user experience.
e0f3bfe unverified
raw
history blame
11.8 kB
<!DOCTYPE html>
<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": {"cell_output": "above", "dataframes": "rich", "default_width": "medium", "theme": "light", "code_editor_font_size": 14}, "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": "medium"}' hidden></marimo-app-config>
<marimo-server-token data-token='123' hidden></marimo-server-token>
<title>003 collections</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 !important;
}
#filename-input {
display: none !important;
}
</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(width%3D%22medium%22)%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%93%A6%20Collections%0A%0A%20%20%20%20%20%20%20%20A%20%22collection%22%20is%20a%20type%20of%20variable%20that%20holds%20multiple%20values.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Lists%0A%20%20%20%20%20%20%20%20Lists%20are%20ordered%2C%20mutable%20sequences.%20Create%20them%20using%20square%20brackets%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20fruits%20%3D%20%5B%22apple%22%2C%20%22banana%22%2C%20%22orange%22%5D%0A%20%20%20%20%20%20%20%20numbers%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%5D%0A%20%20%20%20%20%20%20%20mixed%20%3D%20%5B1%2C%20%22hello%22%2C%203.14%2C%20True%5D%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20Below%20is%20an%20example%20list%20we'll%20use%20to%20explore%20operations.%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_list%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%5D%0A%20%20%20%20return%20(sample_list%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%20List%20operations%0A%0A%20%20%20%20%20%20%20%20Here%20are%20common%20operations%20you%20can%20perform%20on%20lists.%0A%0A%20%20%20%20%20%20%20%20Try%20changing%20the%20values%20in%20%60sample_list%60%20above%20and%20watch%20the%20results%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_(sample_list)%3A%0A%20%20%20%20len(sample_list)%20%20%23%20List%20length%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(sample_list)%3A%0A%20%20%20%20extended_list%20%3D%20sample_list%20%2B%20%5B6%5D%20%20%23%20Concatenate%20two%20lists%0A%20%20%20%20extended_list%0A%20%20%20%20return%20(extended_list%2C)%0A%0A%0A%40app.cell%0Adef%20_(extended_list)%3A%0A%20%20%20%20extended_list%5B0%5D%20%20%23%20Access%20first%20element%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(extended_list)%3A%0A%20%20%20%20extended_list%5B-1%5D%20%20%23%20Access%20last%20element%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%20Tuples%0A%0A%20%20%20%20%20%20%20%20Tuples%20are%20immutable%20sequences.%20They're%20like%20lists%20that%20can't%20be%20changed%20after%20creation%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%20coordinates%20%3D%20(10%2C%2020)%0A%20%20%20%20return%20(coordinates%2C)%0A%0A%0A%40app.cell%0Adef%20_(coordinates)%3A%0A%20%20%20%20x%2C%20y%20%3D%20coordinates%20%20%23%20Tuple%20unpacking%0A%20%20%20%20x%0A%20%20%20%20return%20x%2C%20y%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%23%23%23%23%20Tuple%20concatenation%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20tuple1%20%3D%20(1%2C%202%2C%203)%0A%20%20%20%20tuple2%20%3D%20(4%2C%205%2C%206)%0A%0A%20%20%20%20tuple3%20%3D%20tuple1%20%2B%20tuple2%0A%20%20%20%20tuple3%0A%20%20%20%20return%20tuple1%2C%20tuple2%2C%20tuple3%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%20Dictionaries%0A%0A%20%20%20%20%20%20%20%20Dictionaries%20store%20key-value%20pairs.%20They're%20perfect%20for%20mapping%20relationships%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%20person%20%3D%20%7B%22name%22%3A%20%22John%20Doe%22%2C%20%22age%22%3A%2025%2C%20%22city%22%3A%20%22New%20York%22%7D%0A%20%20%20%20return%20(person%2C)%0A%0A%0A%40app.cell%0Adef%20_(person)%3A%0A%20%20%20%20person%5B%22name%22%5D%20%20%23%20Access%20value%20by%20key%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(person)%3A%0A%20%20%20%20person.keys()%20%20%23%20Get%20all%20keys%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(person)%3A%0A%20%20%20%20person.values()%20%20%23%20Get%20all%20values%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%20Sets%0A%0A%20%20%20%20%20%20%20%20Sets%20are%20unordered%20collections%20of%20unique%20elements%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%20numbers_set%20%3D%20%7B1%2C%202%2C%203%2C%203%2C%202%2C%201%7D%20%20%23%20Duplicates%20are%20removed%0A%20%20%20%20return%20(numbers_set%2C)%0A%0A%0A%40app.cell%0Adef%20_(numbers_set)%3A%0A%20%20%20%20numbers_set%20%7C%20%7B4%7D%20%20%23%20Add%20a%20new%20element%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%7B1%2C%202%2C%203%7D%20%26%20%7B3%2C%204%2C%205%7D%20%20%23%20Find%20common%20elements%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%20Collection%20methods%20and%20operations%0A%0A%20%20%20%20%20%20%20%20Here%20are%20some%20common%20operations%20across%20collections%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%23%20Lists%0A%20%20%20%20%20%20%20%20my_list%20%3D%20%5B1%2C%202%2C%203%5D%0A%20%20%20%20%20%20%20%20my_list.insert(0%2C%200)%20%20%20%20%20%23%20Insert%20at%20position%0A%20%20%20%20%20%20%20%20my_list.remove(2)%20%20%20%20%20%20%20%20%23%20Remove%20first%20occurrence%0A%20%20%20%20%20%20%20%20my_list.sort()%20%20%20%20%20%20%20%20%20%20%20%23%20Sort%20in%20place%0A%20%20%20%20%20%20%20%20sorted_list%20%3D%20sorted(my_list)%20%20%23%20Return%20new%20sorted%20list%0A%0A%20%20%20%20%20%20%20%20%23%20Dictionaries%0A%20%20%20%20%20%20%20%20my_dict%20%3D%20%7B%22a%22%3A%201%7D%0A%20%20%20%20%20%20%20%20my_dict.update(%7B%22b%22%3A%202%7D)%20%20%23%20Add%20new%20key-value%20pairs%0A%20%20%20%20%20%20%20%20my_dict.get(%22c%22%2C%20%22Not%20found%22)%20%20%23%20Safe%20access%20with%20default%0A%0A%20%20%20%20%20%20%20%20%23%20Sets%0A%20%20%20%20%20%20%20%20set_a%20%3D%20%7B1%2C%202%2C%203%7D%0A%20%20%20%20%20%20%20%20set_b%20%3D%20%7B3%2C%204%2C%205%7D%0A%20%20%20%20%20%20%20%20set_a.union(set_b)%20%20%20%20%20%20%20%23%20Combine%20sets%0A%20%20%20%20%20%20%20%20set_a.difference(set_b)%20%20%23%20Elements%20in%20A%20but%20not%20in%20B%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%20Documentation%0A%0A%20%20%20%20%20%20%20%20See%20the%20official%20%5BPython%20tutorial%20on%20data%20structures%5D(https%3A%2F%2Fdocs.python.org%2F3%2Ftutorial%2Fdatastructures.html)%20for%20more%20in-depth%20information.%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>