marimo-learn / _site /python /001_numbers.html
Haleshot's picture
relevant assets and workflow
1cce1df unverified
raw
history blame
10.5 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": {"code_editor_font_size": 14, "cell_output": "above", "default_width": "medium", "theme": "light", "dataframes": "rich"}, "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>001 numbers</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()%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%A2%20Numbers%0A%0A%20%20%20%20%20%20%20%20This%20tutorial%20provides%20a%20brief%20overview%20of%20working%20with%20numbers.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Number%20Types%0A%0A%20%20%20%20%20%20%20%20Python%20has%20several%20types%20of%20numbers%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20integer%20%3D%2042%20%20%20%20%20%20%20%20%20%20%23%20whole%20numbers%20(int)%0A%20%20%20%20%20%20%20%20decimal%20%3D%203.14%20%20%20%20%20%20%20%20%23%20floating-point%20numbers%20(float)%0A%20%20%20%20%20%20%20%20complex_num%20%3D%202%20%2B%203j%20%20%23%20complex%20numbers%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%20number%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%20number%20%3D%2042%0A%20%20%20%20return%20(number%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%20Basic%20mathematical%20operations%0A%0A%20%20%20%20%20%20%20%20Python%20supports%20all%20standard%20mathematical%20operations.%0A%0A%20%20%20%20%20%20%20%20Try%20changing%20the%20value%20of%20%60number%60%20above%20and%20watch%20how%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_(number)%3A%0A%20%20%20%20number%20%2B%2010%20%20%23%20Addition%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20number%20-%205%20%20%23%20Subtraction%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20number%20*%203%20%20%23%20Multiplication%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20number%20%2F%202%20%20%23%20Division%20(always%20returns%20float)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22Python%20also%20has%20special%20division%20operators%20and%20power%20operations.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20number%20%2F%2F%205%20%20%23%20Floor%20division%20(rounds%20down)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20number%20%25%205%20%20%23%20Modulus%20(remainder)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20number**2%20%20%23%20Exponentiation%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%20Type%20conversion%0A%0A%20%20%20%20%20%20%20%20You%20can%20convert%20between%20different%20number%20types.%20Try%20changing%20these%20values!%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%20decimal_number%20%3D%203.14%0A%20%20%20%20return%20(decimal_number%2C)%0A%0A%0A%40app.cell%0Adef%20_(decimal_number)%3A%0A%20%20%20%20int(decimal_number)%20%20%23%20Convert%20to%20integer%20(truncates%20decimal%20part)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(number)%3A%0A%20%20%20%20float(number)%20%20%23%20Convert%20to%20%22float%22%20or%20decimal%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%20Built-in%20math%20functions%0A%20%20%20%20%20%20%20%20Python%20provides%20many%20useful%20built-in%20functions%20for%20working%20with%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_(number)%3A%0A%20%20%20%20abs(-number)%20%20%23%20Absolute%20value%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20round(3.14159%2C%202)%20%20%23%20Round%20to%202%20decimal%20places%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20max(1%2C%205%2C%203%2C%207%2C%202)%20%20%23%20Find%20maximum%20value%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20min(1%2C%205%2C%203%2C%207%2C%202)%20%20%23%20Find%20minimum%20value%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%20Advanced%20operations%0A%0A%20%20%20%20%20%20%20%20For%20more%20complex%20mathematical%20operations%2C%20use%20Python's%20%5Bmath%20module%5D(https%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Fmath.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%20math%0A%20%20%20%20return%20(math%2C)%0A%0A%0A%40app.cell%0Adef%20_(math)%3A%0A%20%20%20%20math.sqrt(16)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(math)%3A%0A%20%20%20%20math.sin(math.pi%2F2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(math)%3A%0A%20%20%20%20math.cos(0)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(math)%3A%0A%20%20%20%20math.pi%2C%20math.e%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(math)%3A%0A%20%20%20%20math.log10(100)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(math)%3A%0A%20%20%20%20math.log(math.e)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Next%20steps%3A%0A%0A%20%20%20%20-%20Practice%20different%20mathematical%20operations%0A%20%20%20%20-%20Experiment%20with%20type%20conversions%0A%20%20%20%20-%20Try%20out%20the%20math%20module%20functions%0A%0A%20%20%20%20Keep%20calculating!%20%F0%9F%A7%AE%E2%9C%A8%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%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>