Spaces:
Running
Running
Akshay Agrawal
commited on
Commit
·
c214ac4
1
Parent(s):
661b6f9
editing pass
Browse files- Python/001_numbers.py +54 -36
- Python/002_strings.py +126 -93
- Python/003_collections.py +21 -35
- Python/004_conditional_logic.py +96 -55
- Python/005_loops.py +79 -78
- Python/006_dictionaries.py +45 -20
Python/001_numbers.py
CHANGED
|
@@ -7,25 +7,20 @@
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
-
__generated_with = "0.10.
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
| 14 |
-
@app.cell
|
| 15 |
-
def _():
|
| 16 |
-
import marimo as mo
|
| 17 |
-
return (mo,)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
@app.cell(hide_code=True)
|
| 21 |
def _(mo):
|
| 22 |
mo.md(
|
| 23 |
"""
|
| 24 |
# 🔢 Numbers in Python
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
## Number Types
|
|
|
|
| 29 |
Python has several types of numbers:
|
| 30 |
|
| 31 |
```python
|
|
@@ -104,11 +99,11 @@ def _(number):
|
|
| 104 |
|
| 105 |
@app.cell
|
| 106 |
def _(number):
|
| 107 |
-
number
|
| 108 |
return
|
| 109 |
|
| 110 |
|
| 111 |
-
@app.cell
|
| 112 |
def _(mo):
|
| 113 |
mo.md(
|
| 114 |
"""
|
|
@@ -134,7 +129,7 @@ def _(decimal_number):
|
|
| 134 |
|
| 135 |
@app.cell
|
| 136 |
def _(number):
|
| 137 |
-
float(number) # Convert to float
|
| 138 |
return
|
| 139 |
|
| 140 |
|
|
@@ -179,49 +174,72 @@ def _(mo):
|
|
| 179 |
"""
|
| 180 |
## Advanced Math Operations
|
| 181 |
|
| 182 |
-
For more complex mathematical operations, Python's
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
# Square root
|
| 188 |
-
math.sqrt(16) # 4.0
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
| 193 |
|
| 194 |
-
# Constants
|
| 195 |
-
math.pi # 3.141592653589793
|
| 196 |
-
math.e # 2.718281828459045
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
return
|
| 205 |
|
| 206 |
|
| 207 |
@app.cell(hide_code=True)
|
| 208 |
def _(mo):
|
| 209 |
-
|
| 210 |
-
##
|
| 211 |
-
|
| 212 |
-
Next Steps:
|
| 213 |
|
| 214 |
- Practice different mathematical operations
|
| 215 |
-
|
| 216 |
- Experiment with type conversions
|
| 217 |
-
|
| 218 |
- Try out the math module functions
|
| 219 |
|
| 220 |
Keep calculating! 🧮✨
|
| 221 |
""")
|
|
|
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
+
__generated_with = "0.10.19"
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.cell(hide_code=True)
|
| 15 |
def _(mo):
|
| 16 |
mo.md(
|
| 17 |
"""
|
| 18 |
# 🔢 Numbers in Python
|
| 19 |
|
| 20 |
+
This tutorial provides a brief overview of working with numbers.
|
| 21 |
|
| 22 |
## Number Types
|
| 23 |
+
|
| 24 |
Python has several types of numbers:
|
| 25 |
|
| 26 |
```python
|
|
|
|
| 99 |
|
| 100 |
@app.cell
|
| 101 |
def _(number):
|
| 102 |
+
number**2 # Exponentiation
|
| 103 |
return
|
| 104 |
|
| 105 |
|
| 106 |
+
@app.cell(hide_code=True)
|
| 107 |
def _(mo):
|
| 108 |
mo.md(
|
| 109 |
"""
|
|
|
|
| 129 |
|
| 130 |
@app.cell
|
| 131 |
def _(number):
|
| 132 |
+
float(number) # Convert to "float" or decimal
|
| 133 |
return
|
| 134 |
|
| 135 |
|
|
|
|
| 174 |
"""
|
| 175 |
## Advanced Math Operations
|
| 176 |
|
| 177 |
+
For more complex mathematical operations, use Python's [math module](https://docs.python.org/3/library/math.html).
|
| 178 |
+
"""
|
| 179 |
+
)
|
| 180 |
+
return
|
| 181 |
+
|
| 182 |
|
| 183 |
+
@app.cell
|
| 184 |
+
def _():
|
| 185 |
+
import math
|
| 186 |
+
return (math,)
|
| 187 |
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
@app.cell
|
| 190 |
+
def _(math):
|
| 191 |
+
math.sqrt(16)
|
| 192 |
+
return
|
| 193 |
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
@app.cell
|
| 196 |
+
def _(math):
|
| 197 |
+
math.sin(math.pi/2)
|
| 198 |
+
return
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
@app.cell
|
| 202 |
+
def _(math):
|
| 203 |
+
math.cos(0)
|
| 204 |
+
return
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
@app.cell
|
| 208 |
+
def _(math):
|
| 209 |
+
math.pi, math.e
|
| 210 |
+
return
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@app.cell
|
| 214 |
+
def _(math):
|
| 215 |
+
math.log10(100)
|
| 216 |
+
return
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
@app.cell
|
| 220 |
+
def _(math):
|
| 221 |
+
math.log(math.e)
|
| 222 |
return
|
| 223 |
|
| 224 |
|
| 225 |
@app.cell(hide_code=True)
|
| 226 |
def _(mo):
|
| 227 |
+
mo.md("""
|
| 228 |
+
## Next Steps:
|
|
|
|
|
|
|
| 229 |
|
| 230 |
- Practice different mathematical operations
|
|
|
|
| 231 |
- Experiment with type conversions
|
|
|
|
| 232 |
- Try out the math module functions
|
| 233 |
|
| 234 |
Keep calculating! 🧮✨
|
| 235 |
""")
|
| 236 |
+
return
|
| 237 |
|
| 238 |
+
|
| 239 |
+
@app.cell
|
| 240 |
+
def _():
|
| 241 |
+
import marimo as mo
|
| 242 |
+
return (mo,)
|
| 243 |
|
| 244 |
|
| 245 |
if __name__ == "__main__":
|
Python/002_strings.py
CHANGED
|
@@ -7,34 +7,38 @@
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
-
__generated_with = "0.10.
|
| 11 |
-
app = marimo.App()
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
@app.cell
|
| 15 |
-
def _():
|
| 16 |
-
import marimo as mo
|
| 17 |
-
return (mo,)
|
| 18 |
|
| 19 |
|
| 20 |
@app.cell(hide_code=True)
|
| 21 |
def _(mo):
|
| 22 |
mo.md(
|
| 23 |
"""
|
| 24 |
-
#
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
##
|
| 29 |
-
|
| 30 |
|
| 31 |
```python
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
```
|
| 36 |
|
| 37 |
-
Below is an example
|
| 38 |
"""
|
| 39 |
)
|
| 40 |
return
|
|
@@ -42,79 +46,86 @@ def _(mo):
|
|
| 42 |
|
| 43 |
@app.cell
|
| 44 |
def _():
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
@app.cell(hide_code=True)
|
| 50 |
def _(mo):
|
| 51 |
mo.md(
|
| 52 |
"""
|
| 53 |
-
##
|
| 54 |
|
| 55 |
-
|
| 56 |
|
| 57 |
-
Try changing the value of `
|
|
|
|
| 58 |
"""
|
| 59 |
)
|
| 60 |
return
|
| 61 |
|
| 62 |
|
| 63 |
@app.cell
|
| 64 |
-
def _(
|
| 65 |
-
number
|
|
|
|
| 66 |
return
|
| 67 |
|
| 68 |
|
| 69 |
@app.cell
|
| 70 |
-
def _(
|
| 71 |
-
|
| 72 |
return
|
| 73 |
|
| 74 |
|
| 75 |
@app.cell
|
| 76 |
-
def _(
|
| 77 |
-
|
| 78 |
return
|
| 79 |
|
| 80 |
|
| 81 |
@app.cell
|
| 82 |
-
def _(
|
| 83 |
-
|
| 84 |
return
|
| 85 |
|
| 86 |
|
| 87 |
@app.cell(hide_code=True)
|
| 88 |
def _(mo):
|
| 89 |
-
mo.md("""
|
| 90 |
return
|
| 91 |
|
| 92 |
|
| 93 |
@app.cell
|
| 94 |
-
def _(
|
| 95 |
-
|
|
|
|
| 96 |
return
|
| 97 |
|
| 98 |
|
| 99 |
@app.cell
|
| 100 |
-
def _(
|
| 101 |
-
|
| 102 |
return
|
| 103 |
|
| 104 |
|
| 105 |
@app.cell
|
| 106 |
-
def _(
|
| 107 |
-
|
| 108 |
return
|
| 109 |
|
| 110 |
|
| 111 |
-
@app.cell
|
| 112 |
def _(mo):
|
| 113 |
mo.md(
|
| 114 |
"""
|
| 115 |
-
##
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
| 118 |
"""
|
| 119 |
)
|
| 120 |
return
|
|
@@ -122,19 +133,13 @@ def _(mo):
|
|
| 122 |
|
| 123 |
@app.cell
|
| 124 |
def _():
|
| 125 |
-
|
| 126 |
-
return (
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
@app.cell
|
| 130 |
-
def _(decimal_number):
|
| 131 |
-
int(decimal_number) # Convert to integer (truncates decimal part)
|
| 132 |
-
return
|
| 133 |
|
| 134 |
|
| 135 |
@app.cell
|
| 136 |
-
def _(
|
| 137 |
-
|
| 138 |
return
|
| 139 |
|
| 140 |
|
|
@@ -142,86 +147,114 @@ def _(number):
|
|
| 142 |
def _(mo):
|
| 143 |
mo.md(
|
| 144 |
"""
|
| 145 |
-
##
|
| 146 |
-
|
| 147 |
"""
|
| 148 |
)
|
| 149 |
return
|
| 150 |
|
| 151 |
|
| 152 |
@app.cell
|
| 153 |
-
def _(
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
|
| 158 |
@app.cell
|
| 159 |
-
def _():
|
| 160 |
-
|
| 161 |
-
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
@app.cell
|
| 165 |
-
def _():
|
| 166 |
-
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
|
| 170 |
@app.cell
|
| 171 |
-
def _():
|
| 172 |
-
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
|
| 176 |
@app.cell(hide_code=True)
|
| 177 |
def _(mo):
|
| 178 |
mo.md(
|
| 179 |
"""
|
| 180 |
-
##
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
```python
|
| 185 |
-
import math
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
# Trigonometry
|
| 191 |
-
math.sin(math.pi/2) # 1.0
|
| 192 |
-
math.cos(0) # 1.0
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
)
|
| 204 |
return
|
| 205 |
|
| 206 |
|
| 207 |
-
@app.cell
|
| 208 |
-
def _(
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
Next Steps:
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
-
- Experiment with type conversions
|
| 217 |
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
+
__generated_with = "0.10.19"
|
| 11 |
+
app = marimo.App(width="medium")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
@app.cell(hide_code=True)
|
| 15 |
def _(mo):
|
| 16 |
mo.md(
|
| 17 |
"""
|
| 18 |
+
# 🎭 Strings
|
| 19 |
|
| 20 |
+
This notebook introduces **strings**, which are containers for text.
|
| 21 |
|
| 22 |
+
## Creating strings
|
| 23 |
+
Create strings by wrapping text in quotes:
|
| 24 |
|
| 25 |
```python
|
| 26 |
+
# Use double quotes
|
| 27 |
+
greeting = "Hello, Python!"
|
| 28 |
+
|
| 29 |
+
# or single quotes
|
| 30 |
+
name = 'Alice'
|
| 31 |
+
|
| 32 |
+
# or triple quotes
|
| 33 |
+
multiline_string = \"""
|
| 34 |
+
Dear, Alice,
|
| 35 |
+
Nice to meet you.
|
| 36 |
+
Sincerely,
|
| 37 |
+
Bob.
|
| 38 |
+
\"""
|
| 39 |
```
|
| 40 |
|
| 41 |
+
Below is an example string.
|
| 42 |
"""
|
| 43 |
)
|
| 44 |
return
|
|
|
|
| 46 |
|
| 47 |
@app.cell
|
| 48 |
def _():
|
| 49 |
+
text = "Python is amazing!"
|
| 50 |
+
text
|
| 51 |
+
return (text,)
|
| 52 |
|
| 53 |
|
| 54 |
@app.cell(hide_code=True)
|
| 55 |
def _(mo):
|
| 56 |
mo.md(
|
| 57 |
"""
|
| 58 |
+
## Essential string operations
|
| 59 |
|
| 60 |
+
Here are some methods for working with strings.
|
| 61 |
|
| 62 |
+
Tip: Try changing the value of `text` above, and watch how the
|
| 63 |
+
computed values below change.
|
| 64 |
"""
|
| 65 |
)
|
| 66 |
return
|
| 67 |
|
| 68 |
|
| 69 |
@app.cell
|
| 70 |
+
def _(text):
|
| 71 |
+
# the `len` method returns the number of characters in the string.
|
| 72 |
+
len(text)
|
| 73 |
return
|
| 74 |
|
| 75 |
|
| 76 |
@app.cell
|
| 77 |
+
def _(text):
|
| 78 |
+
text.upper()
|
| 79 |
return
|
| 80 |
|
| 81 |
|
| 82 |
@app.cell
|
| 83 |
+
def _(text):
|
| 84 |
+
text.lower()
|
| 85 |
return
|
| 86 |
|
| 87 |
|
| 88 |
@app.cell
|
| 89 |
+
def _(text):
|
| 90 |
+
text.title()
|
| 91 |
return
|
| 92 |
|
| 93 |
|
| 94 |
@app.cell(hide_code=True)
|
| 95 |
def _(mo):
|
| 96 |
+
mo.md("""Use string methods and the `in` operator to find things in strings.""")
|
| 97 |
return
|
| 98 |
|
| 99 |
|
| 100 |
@app.cell
|
| 101 |
+
def _(text):
|
| 102 |
+
# Returns the index of "is" in the string
|
| 103 |
+
text.find("is")
|
| 104 |
return
|
| 105 |
|
| 106 |
|
| 107 |
@app.cell
|
| 108 |
+
def _(text):
|
| 109 |
+
"Python" in text
|
| 110 |
return
|
| 111 |
|
| 112 |
|
| 113 |
@app.cell
|
| 114 |
+
def _(text):
|
| 115 |
+
"Javascript" in text
|
| 116 |
return
|
| 117 |
|
| 118 |
|
| 119 |
+
@app.cell(hide_code=True)
|
| 120 |
def _(mo):
|
| 121 |
mo.md(
|
| 122 |
"""
|
| 123 |
+
## Inserting values in strings
|
| 124 |
|
| 125 |
+
Modern Python uses f-strings to insert values into strings. For example,
|
| 126 |
+
check out how the next cell greets you (and notice the `f''''`)!
|
| 127 |
+
|
| 128 |
+
**Try it!** Enter your name in `my_name` below, then run the cell.
|
| 129 |
"""
|
| 130 |
)
|
| 131 |
return
|
|
|
|
| 133 |
|
| 134 |
@app.cell
|
| 135 |
def _():
|
| 136 |
+
my_name = ""
|
| 137 |
+
return (my_name,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
@app.cell
|
| 141 |
+
def _(my_name):
|
| 142 |
+
f"Hello, {my_name}!"
|
| 143 |
return
|
| 144 |
|
| 145 |
|
|
|
|
| 147 |
def _(mo):
|
| 148 |
mo.md(
|
| 149 |
"""
|
| 150 |
+
## Working with Parts of Strings
|
| 151 |
+
You can access any part of a string using its position (index):
|
| 152 |
"""
|
| 153 |
)
|
| 154 |
return
|
| 155 |
|
| 156 |
|
| 157 |
@app.cell
|
| 158 |
+
def _(text):
|
| 159 |
+
first_letter = text[0]
|
| 160 |
+
first_letter
|
| 161 |
+
return (first_letter,)
|
| 162 |
|
| 163 |
|
| 164 |
@app.cell
|
| 165 |
+
def _(text):
|
| 166 |
+
last_letter = text[-1]
|
| 167 |
+
last_letter
|
| 168 |
+
return (last_letter,)
|
| 169 |
|
| 170 |
|
| 171 |
@app.cell
|
| 172 |
+
def _(text):
|
| 173 |
+
first_three = text[0:3]
|
| 174 |
+
first_three
|
| 175 |
+
return (first_three,)
|
| 176 |
|
| 177 |
|
| 178 |
@app.cell
|
| 179 |
+
def _(text):
|
| 180 |
+
last_two = text[-2:]
|
| 181 |
+
last_two
|
| 182 |
+
return (last_two,)
|
| 183 |
|
| 184 |
|
| 185 |
@app.cell(hide_code=True)
|
| 186 |
def _(mo):
|
| 187 |
mo.md(
|
| 188 |
"""
|
| 189 |
+
## Other helpful string methods
|
| 190 |
|
| 191 |
+
Finally, here are some other helpful string methods. Feel free to try them out on your own strings by modifying the value of `sentence` below.
|
| 192 |
+
"""
|
| 193 |
+
)
|
| 194 |
+
return
|
| 195 |
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
@app.cell
|
| 198 |
+
def _():
|
| 199 |
+
sentence = " python is fun "
|
| 200 |
+
sentence
|
| 201 |
+
return (sentence,)
|
| 202 |
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
@app.cell
|
| 205 |
+
def _(sentence):
|
| 206 |
+
# Remove extra spaces
|
| 207 |
+
sentence.strip()
|
| 208 |
+
return
|
| 209 |
|
| 210 |
+
|
| 211 |
+
@app.cell
|
| 212 |
+
def _(sentence):
|
| 213 |
+
# Split into a list of words
|
| 214 |
+
sentence.split()
|
|
|
|
| 215 |
return
|
| 216 |
|
| 217 |
|
| 218 |
+
@app.cell
|
| 219 |
+
def _(sentence):
|
| 220 |
+
sentence.replace("fun", "awesome")
|
| 221 |
+
return
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
@app.cell
|
| 225 |
+
def _():
|
| 226 |
+
"123".isdigit(), "abc".isdigit()
|
| 227 |
+
return
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
@app.cell
|
| 231 |
+
def _():
|
| 232 |
+
"123".isalpha(), "abc".isalpha()
|
| 233 |
+
return
|
| 234 |
|
|
|
|
| 235 |
|
| 236 |
+
@app.cell
|
| 237 |
+
def _():
|
| 238 |
+
"Python3".isalnum()
|
| 239 |
+
return
|
| 240 |
|
|
|
|
| 241 |
|
| 242 |
+
@app.cell(hide_code=True)
|
| 243 |
+
def _(mo):
|
| 244 |
+
mo.md(
|
| 245 |
+
r"""
|
| 246 |
+
## Next steps
|
| 247 |
|
| 248 |
+
For a full primer on strings, check out the [official documentation](https://docs.python.org/3/library/string.html).
|
| 249 |
+
"""
|
| 250 |
+
)
|
| 251 |
+
return
|
| 252 |
|
| 253 |
+
|
| 254 |
+
@app.cell
|
| 255 |
+
def _():
|
| 256 |
+
import marimo as mo
|
| 257 |
+
return (mo,)
|
| 258 |
|
| 259 |
|
| 260 |
if __name__ == "__main__":
|
Python/003_collections.py
CHANGED
|
@@ -7,23 +7,17 @@
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
-
__generated_with = "0.10.
|
| 11 |
-
app = marimo.App()
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
@app.cell
|
| 15 |
-
def _():
|
| 16 |
-
import marimo as mo
|
| 17 |
-
return (mo,)
|
| 18 |
|
| 19 |
|
| 20 |
@app.cell(hide_code=True)
|
| 21 |
def _(mo):
|
| 22 |
mo.md(
|
| 23 |
"""
|
| 24 |
-
# 📦 Collections
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
## Lists
|
| 29 |
Lists are ordered, mutable sequences. Create them using square brackets:
|
|
@@ -31,7 +25,7 @@ def _(mo):
|
|
| 31 |
```python
|
| 32 |
fruits = ["apple", "banana", "orange"]
|
| 33 |
numbers = [1, 2, 3, 4, 5]
|
| 34 |
-
mixed = [1, "hello", 3.14, True]
|
| 35 |
```
|
| 36 |
|
| 37 |
Below is an example list we'll use to explore operations.
|
|
@@ -68,7 +62,7 @@ def _(sample_list):
|
|
| 68 |
|
| 69 |
@app.cell
|
| 70 |
def _(sample_list):
|
| 71 |
-
extended_list = sample_list + [6]
|
| 72 |
extended_list
|
| 73 |
return (extended_list,)
|
| 74 |
|
|
@@ -122,7 +116,6 @@ def _():
|
|
| 122 |
tuple2 = (4, 5, 6)
|
| 123 |
|
| 124 |
tuple3 = tuple1 + tuple2
|
| 125 |
-
|
| 126 |
tuple3
|
| 127 |
return tuple1, tuple2, tuple3
|
| 128 |
|
|
@@ -141,11 +134,7 @@ def _(mo):
|
|
| 141 |
|
| 142 |
@app.cell
|
| 143 |
def _():
|
| 144 |
-
person = {
|
| 145 |
-
"name": "John Doe",
|
| 146 |
-
"age": 25,
|
| 147 |
-
"city": "New York"
|
| 148 |
-
}
|
| 149 |
return (person,)
|
| 150 |
|
| 151 |
|
|
@@ -187,17 +176,14 @@ def _():
|
|
| 187 |
|
| 188 |
@app.cell
|
| 189 |
def _(numbers_set):
|
| 190 |
-
numbers_set
|
| 191 |
-
numbers_set
|
| 192 |
return
|
| 193 |
|
| 194 |
|
| 195 |
@app.cell
|
| 196 |
def _():
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
set1.intersection(set2) # Find common elements
|
| 200 |
-
return set1, set2
|
| 201 |
|
| 202 |
|
| 203 |
@app.cell(hide_code=True)
|
|
@@ -234,20 +220,20 @@ def _(mo):
|
|
| 234 |
|
| 235 |
@app.cell(hide_code=True)
|
| 236 |
def _(mo):
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
Next Steps:
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
|
|
|
| 245 |
|
| 246 |
-
Keep organizing data! 🗃️✨
|
| 247 |
-
""")
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
+
__generated_with = "0.10.19"
|
| 11 |
+
app = marimo.App(width="medium")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
@app.cell(hide_code=True)
|
| 15 |
def _(mo):
|
| 16 |
mo.md(
|
| 17 |
"""
|
| 18 |
+
# 📦 Collections
|
| 19 |
|
| 20 |
+
A "collection" is a type of variable that holds multiple values.
|
| 21 |
|
| 22 |
## Lists
|
| 23 |
Lists are ordered, mutable sequences. Create them using square brackets:
|
|
|
|
| 25 |
```python
|
| 26 |
fruits = ["apple", "banana", "orange"]
|
| 27 |
numbers = [1, 2, 3, 4, 5]
|
| 28 |
+
mixed = [1, "hello", 3.14, True]
|
| 29 |
```
|
| 30 |
|
| 31 |
Below is an example list we'll use to explore operations.
|
|
|
|
| 62 |
|
| 63 |
@app.cell
|
| 64 |
def _(sample_list):
|
| 65 |
+
extended_list = sample_list + [6] # Concatenate two lists
|
| 66 |
extended_list
|
| 67 |
return (extended_list,)
|
| 68 |
|
|
|
|
| 116 |
tuple2 = (4, 5, 6)
|
| 117 |
|
| 118 |
tuple3 = tuple1 + tuple2
|
|
|
|
| 119 |
tuple3
|
| 120 |
return tuple1, tuple2, tuple3
|
| 121 |
|
|
|
|
| 134 |
|
| 135 |
@app.cell
|
| 136 |
def _():
|
| 137 |
+
person = {"name": "John Doe", "age": 25, "city": "New York"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
return (person,)
|
| 139 |
|
| 140 |
|
|
|
|
| 176 |
|
| 177 |
@app.cell
|
| 178 |
def _(numbers_set):
|
| 179 |
+
numbers_set | {4} # Add a new element
|
|
|
|
| 180 |
return
|
| 181 |
|
| 182 |
|
| 183 |
@app.cell
|
| 184 |
def _():
|
| 185 |
+
{1, 2, 3} & {3, 4, 5} # Find common elements
|
| 186 |
+
return
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
@app.cell(hide_code=True)
|
|
|
|
| 220 |
|
| 221 |
@app.cell(hide_code=True)
|
| 222 |
def _(mo):
|
| 223 |
+
mo.md(
|
| 224 |
+
r"""
|
| 225 |
+
## Documentation
|
|
|
|
| 226 |
|
| 227 |
+
See the official [Python tutorial on data structures](https://docs.python.org/3/tutorial/datastructures.html) for more in-depth information.
|
| 228 |
+
"""
|
| 229 |
+
)
|
| 230 |
+
return
|
| 231 |
|
|
|
|
|
|
|
| 232 |
|
| 233 |
+
@app.cell
|
| 234 |
+
def _():
|
| 235 |
+
import marimo as mo
|
| 236 |
+
return (mo,)
|
| 237 |
|
| 238 |
|
| 239 |
if __name__ == "__main__":
|
Python/004_conditional_logic.py
CHANGED
|
@@ -7,23 +7,18 @@
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
-
__generated_with = "0.10.
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
| 14 |
-
@app.cell
|
| 15 |
-
def _():
|
| 16 |
-
import marimo as mo
|
| 17 |
-
return (mo,)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
@app.cell(hide_code=True)
|
| 21 |
def _(mo):
|
| 22 |
mo.md(
|
| 23 |
"""
|
| 24 |
# 🔄 Conditional Logic in Python
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
## If Statements
|
| 29 |
The foundation of decision-making in Python:
|
|
@@ -41,21 +36,43 @@ def _(mo):
|
|
| 41 |
return
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
@app.cell
|
| 45 |
def _():
|
| 46 |
number = 42
|
| 47 |
return (number,)
|
| 48 |
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
@app.cell
|
| 51 |
-
def _(number):
|
| 52 |
-
if number >
|
| 53 |
-
result = "Greater than
|
| 54 |
elif number == 42:
|
| 55 |
-
result = "
|
| 56 |
else:
|
| 57 |
-
result = "Less than
|
| 58 |
-
result
|
| 59 |
return (result,)
|
| 60 |
|
| 61 |
|
|
@@ -63,27 +80,27 @@ def _(number):
|
|
| 63 |
def _(mo):
|
| 64 |
mo.md(
|
| 65 |
r"""
|
| 66 |
-
|
| 67 |
-
Try changing the conditions below and see how the results change
|
| 68 |
"""
|
| 69 |
)
|
| 70 |
return
|
| 71 |
|
| 72 |
|
| 73 |
-
@app.cell
|
| 74 |
def _(mo, threshold, value):
|
| 75 |
-
mo.hstack([value, threshold])
|
| 76 |
return
|
| 77 |
|
| 78 |
|
| 79 |
-
@app.cell
|
| 80 |
def _(mo):
|
| 81 |
value = mo.ui.number(value=25, start=0, stop=100, label="Enter a number")
|
| 82 |
threshold = mo.ui.slider(value=50, start=0, stop=100, label="Set threshold")
|
| 83 |
return threshold, value
|
| 84 |
|
| 85 |
|
| 86 |
-
@app.cell
|
| 87 |
def _(mo, threshold, value):
|
| 88 |
if value.value > threshold.value:
|
| 89 |
decision = f"{value.value} is greater than {threshold.value}"
|
|
@@ -95,9 +112,11 @@ def _(mo, threshold, value):
|
|
| 95 |
mo.hstack(
|
| 96 |
[
|
| 97 |
mo.md(f"**Decision**: {decision}"),
|
| 98 |
-
mo.md(
|
|
|
|
|
|
|
| 99 |
],
|
| 100 |
-
justify="space-
|
| 101 |
)
|
| 102 |
return (decision,)
|
| 103 |
|
|
@@ -119,20 +138,31 @@ def _(mo):
|
|
| 119 |
return
|
| 120 |
|
| 121 |
|
| 122 |
-
@app.cell
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def _(age, has_id, mo):
|
| 124 |
-
mo.hstack([age, has_id])
|
| 125 |
return
|
| 126 |
|
| 127 |
|
| 128 |
-
@app.cell
|
| 129 |
def _(mo):
|
| 130 |
age = mo.ui.number(value=18, start=0, stop=120, label="Age")
|
| 131 |
has_id = mo.ui.switch(value=True, label="Has ID")
|
| 132 |
return age, has_id
|
| 133 |
|
| 134 |
|
| 135 |
-
@app.cell
|
| 136 |
def _(age, has_id, mo):
|
| 137 |
can_vote = age.value >= 18 and has_id.value
|
| 138 |
|
|
@@ -143,12 +173,15 @@ def _(age, has_id, mo):
|
|
| 143 |
|
| 144 |
- Age: {age.value} years old
|
| 145 |
|
| 146 |
-
- Has ID: {
|
| 147 |
|
| 148 |
-
- Can Vote: {
|
| 149 |
|
| 150 |
-
Reason: {
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
"""
|
| 153 |
|
| 154 |
mo.md(explanation)
|
|
@@ -157,15 +190,22 @@ def _(age, has_id, mo):
|
|
| 157 |
|
| 158 |
@app.cell(hide_code=True)
|
| 159 |
def _(mo):
|
| 160 |
-
|
| 161 |
-
- Try different combinations of age and ID status
|
| 162 |
-
- Notice how both conditions must be True to allow voting
|
| 163 |
-
- Experiment with edge cases (exactly 18, no ID, etc.)
|
| 164 |
-
""")
|
| 165 |
-
mo.accordion({"💡 Experiment Tips": _text})
|
| 166 |
return
|
| 167 |
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
@app.cell(hide_code=True)
|
| 170 |
def _(mo):
|
| 171 |
mo.md(
|
|
@@ -189,13 +229,13 @@ def _(mo):
|
|
| 189 |
return
|
| 190 |
|
| 191 |
|
| 192 |
-
@app.cell
|
| 193 |
def _(humidity, mo, temp, wind):
|
| 194 |
mo.hstack([temp, humidity, wind])
|
| 195 |
return
|
| 196 |
|
| 197 |
|
| 198 |
-
@app.cell
|
| 199 |
def _(mo):
|
| 200 |
temp = mo.ui.number(value=25, start=-20, stop=50, label="Temperature (°C)")
|
| 201 |
humidity = mo.ui.slider(value=60, start=0, stop=100, label="Humidity (%)")
|
|
@@ -203,28 +243,29 @@ def _(mo):
|
|
| 203 |
return humidity, temp, wind
|
| 204 |
|
| 205 |
|
| 206 |
-
@app.cell
|
| 207 |
def _(humidity, mo, temp, wind):
|
| 208 |
def get_weather_advice():
|
| 209 |
conditions = []
|
| 210 |
-
|
| 211 |
if temp.value > 30:
|
| 212 |
conditions.append("🌡️ High temperature")
|
| 213 |
elif temp.value < 10:
|
| 214 |
conditions.append("❄️ Cold temperature")
|
| 215 |
-
|
| 216 |
if humidity.value > 80:
|
| 217 |
conditions.append("💧 High humidity")
|
| 218 |
elif humidity.value < 30:
|
| 219 |
conditions.append("🏜️ Low humidity")
|
| 220 |
-
|
| 221 |
if wind.value > 30:
|
| 222 |
conditions.append("💨 Strong winds")
|
| 223 |
-
|
| 224 |
return conditions
|
| 225 |
-
|
|
|
|
| 226 |
conditions = get_weather_advice()
|
| 227 |
-
|
| 228 |
message = f"""
|
| 229 |
### Weather Analysis
|
| 230 |
|
|
@@ -236,7 +277,7 @@ def _(humidity, mo, temp, wind):
|
|
| 236 |
|
| 237 |
- Wind Speed: {wind.value} km/h
|
| 238 |
|
| 239 |
-
Alerts: {
|
| 240 |
"""
|
| 241 |
|
| 242 |
mo.md(message)
|
|
@@ -245,22 +286,22 @@ def _(humidity, mo, temp, wind):
|
|
| 245 |
|
| 246 |
@app.cell(hide_code=True)
|
| 247 |
def _(mo):
|
| 248 |
-
|
| 249 |
-
##
|
| 250 |
-
|
| 251 |
-
Next Steps:
|
| 252 |
|
| 253 |
- Practice combining multiple conditions
|
| 254 |
-
|
| 255 |
- Explore nested if statements
|
| 256 |
-
|
| 257 |
-
- Try creating your own complex decision trees (pun on an ML algorithm!)
|
| 258 |
|
| 259 |
Keep coding! 🎯✨
|
| 260 |
""")
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
|
| 266 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
+
__generated_with = "0.10.19"
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.cell(hide_code=True)
|
| 15 |
def _(mo):
|
| 16 |
mo.md(
|
| 17 |
"""
|
| 18 |
# 🔄 Conditional Logic in Python
|
| 19 |
|
| 20 |
+
This tutorial teaches you how to how to make **decisions** in your code, using
|
| 21 |
+
Python's conditional statements.
|
| 22 |
|
| 23 |
## If Statements
|
| 24 |
The foundation of decision-making in Python:
|
|
|
|
| 36 |
return
|
| 37 |
|
| 38 |
|
| 39 |
+
@app.cell(hide_code=True)
|
| 40 |
+
def _(mo):
|
| 41 |
+
mo.md("""**Try it!** Try changing the value of `42` below, and see how the output changes.""")
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
+
|
| 45 |
@app.cell
|
| 46 |
def _():
|
| 47 |
number = 42
|
| 48 |
return (number,)
|
| 49 |
|
| 50 |
|
| 51 |
+
@app.cell(hide_code=True)
|
| 52 |
+
def _(mo):
|
| 53 |
+
mo.md(
|
| 54 |
+
r"""
|
| 55 |
+
Compare numbers using operators like
|
| 56 |
+
|
| 57 |
+
- `>`
|
| 58 |
+
- `>=`
|
| 59 |
+
- `<`
|
| 60 |
+
- `<=`
|
| 61 |
+
- `==` (note the two equal signs!)
|
| 62 |
+
"""
|
| 63 |
+
)
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
|
| 67 |
@app.cell
|
| 68 |
+
def _(mo, number):
|
| 69 |
+
if number > 42:
|
| 70 |
+
result = "Greater than 42"
|
| 71 |
elif number == 42:
|
| 72 |
+
result = "Equal to 42!"
|
| 73 |
else:
|
| 74 |
+
result = "Less than 42"
|
| 75 |
+
mo.md(result)
|
| 76 |
return (result,)
|
| 77 |
|
| 78 |
|
|
|
|
| 80 |
def _(mo):
|
| 81 |
mo.md(
|
| 82 |
r"""
|
| 83 |
+
### Interactive Decision Making
|
| 84 |
+
**Try it!** Try changing the conditions below and see how the results change:
|
| 85 |
"""
|
| 86 |
)
|
| 87 |
return
|
| 88 |
|
| 89 |
|
| 90 |
+
@app.cell(hide_code=True)
|
| 91 |
def _(mo, threshold, value):
|
| 92 |
+
mo.hstack([value, threshold], justify="start")
|
| 93 |
return
|
| 94 |
|
| 95 |
|
| 96 |
+
@app.cell(hide_code=True)
|
| 97 |
def _(mo):
|
| 98 |
value = mo.ui.number(value=25, start=0, stop=100, label="Enter a number")
|
| 99 |
threshold = mo.ui.slider(value=50, start=0, stop=100, label="Set threshold")
|
| 100 |
return threshold, value
|
| 101 |
|
| 102 |
|
| 103 |
+
@app.cell(hide_code=True)
|
| 104 |
def _(mo, threshold, value):
|
| 105 |
if value.value > threshold.value:
|
| 106 |
decision = f"{value.value} is greater than {threshold.value}"
|
|
|
|
| 112 |
mo.hstack(
|
| 113 |
[
|
| 114 |
mo.md(f"**Decision**: {decision}"),
|
| 115 |
+
mo.md(
|
| 116 |
+
f"**Threshold cleared?**: {'✅' if value.value >= threshold.value else '❌'}"
|
| 117 |
+
),
|
| 118 |
],
|
| 119 |
+
justify="space-around",
|
| 120 |
)
|
| 121 |
return (decision,)
|
| 122 |
|
|
|
|
| 138 |
return
|
| 139 |
|
| 140 |
|
| 141 |
+
@app.cell(hide_code=True)
|
| 142 |
+
def _(mo):
|
| 143 |
+
_text = mo.md("""
|
| 144 |
+
- Try different combinations of age and ID status
|
| 145 |
+
- Notice how both conditions must be True to allow voting
|
| 146 |
+
- Experiment with edge cases (exactly 18, no ID, etc.)
|
| 147 |
+
""")
|
| 148 |
+
mo.accordion({"💡 Experiment Tips": _text})
|
| 149 |
+
return
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@app.cell(hide_code=True)
|
| 153 |
def _(age, has_id, mo):
|
| 154 |
+
mo.hstack([age, has_id], justify="start")
|
| 155 |
return
|
| 156 |
|
| 157 |
|
| 158 |
+
@app.cell(hide_code=True)
|
| 159 |
def _(mo):
|
| 160 |
age = mo.ui.number(value=18, start=0, stop=120, label="Age")
|
| 161 |
has_id = mo.ui.switch(value=True, label="Has ID")
|
| 162 |
return age, has_id
|
| 163 |
|
| 164 |
|
| 165 |
+
@app.cell(hide_code=True)
|
| 166 |
def _(age, has_id, mo):
|
| 167 |
can_vote = age.value >= 18 and has_id.value
|
| 168 |
|
|
|
|
| 173 |
|
| 174 |
- Age: {age.value} years old
|
| 175 |
|
| 176 |
+
- Has ID: {"Yes" if has_id.value else "No"}
|
| 177 |
|
| 178 |
+
- Can Vote: {"Yes ✅" if can_vote else "No ❌"}
|
| 179 |
|
| 180 |
+
Reason: {
|
| 181 |
+
"Both age and ID requirements met"
|
| 182 |
+
if can_vote
|
| 183 |
+
else "Missing " + ("required age" if age.value < 18 else "valid ID")
|
| 184 |
+
}
|
| 185 |
"""
|
| 186 |
|
| 187 |
mo.md(explanation)
|
|
|
|
| 190 |
|
| 191 |
@app.cell(hide_code=True)
|
| 192 |
def _(mo):
|
| 193 |
+
mo.md(r"""**Try it!** Write Python code that computes whether an individual can vote.""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
return
|
| 195 |
|
| 196 |
|
| 197 |
+
@app.cell
|
| 198 |
+
def _():
|
| 199 |
+
my_age = 18
|
| 200 |
+
return (my_age,)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
@app.cell
|
| 204 |
+
def _():
|
| 205 |
+
has_an_id = False
|
| 206 |
+
return (has_an_id,)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
@app.cell(hide_code=True)
|
| 210 |
def _(mo):
|
| 211 |
mo.md(
|
|
|
|
| 229 |
return
|
| 230 |
|
| 231 |
|
| 232 |
+
@app.cell(hide_code=True)
|
| 233 |
def _(humidity, mo, temp, wind):
|
| 234 |
mo.hstack([temp, humidity, wind])
|
| 235 |
return
|
| 236 |
|
| 237 |
|
| 238 |
+
@app.cell(hide_code=True)
|
| 239 |
def _(mo):
|
| 240 |
temp = mo.ui.number(value=25, start=-20, stop=50, label="Temperature (°C)")
|
| 241 |
humidity = mo.ui.slider(value=60, start=0, stop=100, label="Humidity (%)")
|
|
|
|
| 243 |
return humidity, temp, wind
|
| 244 |
|
| 245 |
|
| 246 |
+
@app.cell(hide_code=True)
|
| 247 |
def _(humidity, mo, temp, wind):
|
| 248 |
def get_weather_advice():
|
| 249 |
conditions = []
|
| 250 |
+
|
| 251 |
if temp.value > 30:
|
| 252 |
conditions.append("🌡️ High temperature")
|
| 253 |
elif temp.value < 10:
|
| 254 |
conditions.append("❄️ Cold temperature")
|
| 255 |
+
|
| 256 |
if humidity.value > 80:
|
| 257 |
conditions.append("💧 High humidity")
|
| 258 |
elif humidity.value < 30:
|
| 259 |
conditions.append("🏜️ Low humidity")
|
| 260 |
+
|
| 261 |
if wind.value > 30:
|
| 262 |
conditions.append("💨 Strong winds")
|
| 263 |
+
|
| 264 |
return conditions
|
| 265 |
+
|
| 266 |
+
|
| 267 |
conditions = get_weather_advice()
|
| 268 |
+
|
| 269 |
message = f"""
|
| 270 |
### Weather Analysis
|
| 271 |
|
|
|
|
| 277 |
|
| 278 |
- Wind Speed: {wind.value} km/h
|
| 279 |
|
| 280 |
+
Alerts: {", ".join(conditions) if conditions else "No special alerts"}
|
| 281 |
"""
|
| 282 |
|
| 283 |
mo.md(message)
|
|
|
|
| 286 |
|
| 287 |
@app.cell(hide_code=True)
|
| 288 |
def _(mo):
|
| 289 |
+
mo.md("""
|
| 290 |
+
## Next steps
|
|
|
|
|
|
|
| 291 |
|
| 292 |
- Practice combining multiple conditions
|
|
|
|
| 293 |
- Explore nested if statements
|
| 294 |
+
- Try creating your own complex decision trees
|
|
|
|
| 295 |
|
| 296 |
Keep coding! 🎯✨
|
| 297 |
""")
|
| 298 |
+
return
|
| 299 |
|
| 300 |
+
|
| 301 |
+
@app.cell
|
| 302 |
+
def _():
|
| 303 |
+
import marimo as mo
|
| 304 |
+
return (mo,)
|
| 305 |
|
| 306 |
|
| 307 |
if __name__ == "__main__":
|
Python/005_loops.py
CHANGED
|
@@ -7,38 +7,32 @@
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
-
__generated_with = "0.10.
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
| 14 |
-
@app.cell
|
| 15 |
-
def _():
|
| 16 |
-
import marimo as mo
|
| 17 |
-
return (mo,)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
@app.cell(hide_code=True)
|
| 21 |
def _(mo):
|
| 22 |
mo.md(
|
| 23 |
"""
|
| 24 |
# 🔄 Loops in Python
|
| 25 |
|
| 26 |
-
Let's
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
```python
|
| 32 |
-
# For loop
|
| 33 |
for i in range(5):
|
| 34 |
print(i)
|
| 35 |
|
| 36 |
-
# While loop
|
| 37 |
while condition:
|
| 38 |
do_something()
|
| 39 |
```
|
| 40 |
|
| 41 |
-
Let's start with a simple list to explore loops.
|
| 42 |
"""
|
| 43 |
)
|
| 44 |
return
|
|
@@ -54,7 +48,7 @@ def _():
|
|
| 54 |
def _(mo):
|
| 55 |
mo.md(
|
| 56 |
"""
|
| 57 |
-
##
|
| 58 |
|
| 59 |
The for loop is perfect for iterating over sequences.
|
| 60 |
Try changing the `sample_fruits` list above and see how the output changes.
|
|
@@ -65,122 +59,128 @@ def _(mo):
|
|
| 65 |
|
| 66 |
@app.cell
|
| 67 |
def _(sample_fruits):
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
print(f"I like {_fruit}s!")
|
| 71 |
-
_print_fruits()
|
| 72 |
return
|
| 73 |
|
| 74 |
|
| 75 |
@app.cell(hide_code=True)
|
| 76 |
def _(mo):
|
| 77 |
-
mo.md(
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
When you need both the item and its position, use enumerate()
|
| 81 |
-
"""
|
|
|
|
| 82 |
return
|
| 83 |
|
| 84 |
|
| 85 |
@app.cell
|
| 86 |
def _(sample_fruits):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
print(f"{_idx + 1}. {_fruit}")
|
| 90 |
-
_print_enumerated()
|
| 91 |
return
|
| 92 |
|
| 93 |
|
| 94 |
@app.cell(hide_code=True)
|
| 95 |
def _(mo):
|
| 96 |
-
mo.md(
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
return
|
| 102 |
|
| 103 |
|
| 104 |
@app.cell
|
| 105 |
def _():
|
| 106 |
-
|
| 107 |
-
print(
|
| 108 |
-
print("range(2, 5):", list(range(2, 5)))
|
| 109 |
-
print("range(0, 10, 2):", list(range(0, 10, 2)))
|
| 110 |
-
_demonstrate_range()
|
| 111 |
return
|
| 112 |
|
| 113 |
|
| 114 |
@app.cell(hide_code=True)
|
| 115 |
def _(mo):
|
| 116 |
-
mo.md(
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
return
|
| 120 |
|
| 121 |
|
| 122 |
@app.cell
|
| 123 |
def _():
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
_count += 1
|
| 129 |
-
_count_up()
|
| 130 |
return
|
| 131 |
|
| 132 |
|
| 133 |
@app.cell(hide_code=True)
|
| 134 |
def _(mo):
|
| 135 |
-
mo.md(
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
Python provides several ways to control loop execution:
|
| 139 |
|
| 140 |
-
- `break`:
|
| 141 |
|
| 142 |
-
- `continue`:
|
| 143 |
|
| 144 |
-
|
| 145 |
-
"""
|
|
|
|
| 146 |
return
|
| 147 |
|
| 148 |
|
| 149 |
@app.cell
|
| 150 |
def _():
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
print("Loop ended early!")
|
| 157 |
-
_demonstrate_break()
|
| 158 |
return
|
| 159 |
|
| 160 |
|
| 161 |
@app.cell
|
| 162 |
def _():
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
print(_i)
|
| 168 |
-
_demonstrate_continue()
|
| 169 |
return
|
| 170 |
|
| 171 |
|
| 172 |
@app.cell(hide_code=True)
|
| 173 |
def _(mo):
|
| 174 |
-
mo.md(
|
| 175 |
-
|
|
|
|
| 176 |
|
| 177 |
Here are some common patterns you'll use with loops:
|
| 178 |
|
| 179 |
```python
|
| 180 |
# Pattern 1: Accumulator
|
| 181 |
-
|
| 182 |
for num in [1, 2, 3, 4, 5]:
|
| 183 |
-
|
| 184 |
|
| 185 |
# Pattern 2: Search
|
| 186 |
found = False
|
|
@@ -195,26 +195,27 @@ def _(mo):
|
|
| 195 |
if condition:
|
| 196 |
filtered.append(item)
|
| 197 |
```
|
| 198 |
-
"""
|
|
|
|
| 199 |
return
|
| 200 |
|
| 201 |
|
| 202 |
@app.cell(hide_code=True)
|
| 203 |
def _(mo):
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
Next Steps:
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
| 212 |
|
| 213 |
-
Keep iterating! 🔄✨
|
| 214 |
-
""")
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
| 218 |
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
+
__generated_with = "0.10.19"
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.cell(hide_code=True)
|
| 15 |
def _(mo):
|
| 16 |
mo.md(
|
| 17 |
"""
|
| 18 |
# 🔄 Loops in Python
|
| 19 |
|
| 20 |
+
Let's learn how Python helps us repeat tasks efficiently with loops.
|
| 21 |
|
| 22 |
+
A "loop" is a way to execute a block of code multiple times. Python has two
|
| 23 |
+
main types of loops:
|
| 24 |
|
| 25 |
```python
|
| 26 |
+
# For loop: when you know how many times to repeat
|
| 27 |
for i in range(5):
|
| 28 |
print(i)
|
| 29 |
|
| 30 |
+
# While loop: when you don't know how many repetitions
|
| 31 |
while condition:
|
| 32 |
do_something()
|
| 33 |
```
|
| 34 |
|
| 35 |
+
Let's start with a simple list to explore loops. Feel free to modify this list and see how the subsequent outputs change.
|
| 36 |
"""
|
| 37 |
)
|
| 38 |
return
|
|
|
|
| 48 |
def _(mo):
|
| 49 |
mo.md(
|
| 50 |
"""
|
| 51 |
+
## The for loop
|
| 52 |
|
| 53 |
The for loop is perfect for iterating over sequences.
|
| 54 |
Try changing the `sample_fruits` list above and see how the output changes.
|
|
|
|
| 59 |
|
| 60 |
@app.cell
|
| 61 |
def _(sample_fruits):
|
| 62 |
+
for _fruit in sample_fruits:
|
| 63 |
+
print(f"I like {_fruit}s!")
|
|
|
|
|
|
|
| 64 |
return
|
| 65 |
|
| 66 |
|
| 67 |
@app.cell(hide_code=True)
|
| 68 |
def _(mo):
|
| 69 |
+
mo.md(
|
| 70 |
+
"""
|
| 71 |
+
### Getting the position of an item
|
| 72 |
|
| 73 |
+
When you need both the item and its position, use `enumerate()`:
|
| 74 |
+
"""
|
| 75 |
+
)
|
| 76 |
return
|
| 77 |
|
| 78 |
|
| 79 |
@app.cell
|
| 80 |
def _(sample_fruits):
|
| 81 |
+
for _idx, _fruit in enumerate(sample_fruits):
|
| 82 |
+
print(f"{_idx + 1}. {_fruit}")
|
|
|
|
|
|
|
| 83 |
return
|
| 84 |
|
| 85 |
|
| 86 |
@app.cell(hide_code=True)
|
| 87 |
def _(mo):
|
| 88 |
+
mo.md(
|
| 89 |
+
"""
|
| 90 |
+
### Iterating over a range of numbers
|
| 91 |
+
|
| 92 |
+
`range()` is a powerful function for generating sequences of numbers:
|
| 93 |
+
"""
|
| 94 |
+
)
|
| 95 |
+
return
|
| 96 |
|
| 97 |
+
|
| 98 |
+
@app.cell
|
| 99 |
+
def _():
|
| 100 |
+
print("range(5):", list(range(5)))
|
| 101 |
+
print("range(2, 5):", list(range(2, 5)))
|
| 102 |
+
print("range(0, 10, 2):", list(range(0, 10, 2)))
|
| 103 |
return
|
| 104 |
|
| 105 |
|
| 106 |
@app.cell
|
| 107 |
def _():
|
| 108 |
+
for _i in range(5):
|
| 109 |
+
print(_i)
|
|
|
|
|
|
|
|
|
|
| 110 |
return
|
| 111 |
|
| 112 |
|
| 113 |
@app.cell(hide_code=True)
|
| 114 |
def _(mo):
|
| 115 |
+
mo.md(
|
| 116 |
+
"""
|
| 117 |
+
## The `while` loop
|
| 118 |
|
| 119 |
+
While loops continue as long as a condition is `True`.
|
| 120 |
+
"""
|
| 121 |
+
)
|
| 122 |
return
|
| 123 |
|
| 124 |
|
| 125 |
@app.cell
|
| 126 |
def _():
|
| 127 |
+
_count = 0
|
| 128 |
+
while _count < 5:
|
| 129 |
+
print(f"The count is {_count}")
|
| 130 |
+
_count += 1
|
|
|
|
|
|
|
| 131 |
return
|
| 132 |
|
| 133 |
|
| 134 |
@app.cell(hide_code=True)
|
| 135 |
def _(mo):
|
| 136 |
+
mo.md(
|
| 137 |
+
"""
|
| 138 |
+
## Controlling loop execution
|
| 139 |
|
| 140 |
Python provides several ways to control loop execution:
|
| 141 |
|
| 142 |
+
- `break`: exit the loop immediately
|
| 143 |
|
| 144 |
+
- `continue`: skip to the next iteration
|
| 145 |
|
| 146 |
+
These can be used with both `for` and `while` loops.
|
| 147 |
+
"""
|
| 148 |
+
)
|
| 149 |
return
|
| 150 |
|
| 151 |
|
| 152 |
@app.cell
|
| 153 |
def _():
|
| 154 |
+
for _i in range(1, 6):
|
| 155 |
+
if _i == 4:
|
| 156 |
+
print("Breaking out of the loop.")
|
| 157 |
+
break
|
| 158 |
+
print(_i)
|
|
|
|
|
|
|
| 159 |
return
|
| 160 |
|
| 161 |
|
| 162 |
@app.cell
|
| 163 |
def _():
|
| 164 |
+
for _i in range(1, 6):
|
| 165 |
+
if _i == 3:
|
| 166 |
+
continue
|
| 167 |
+
print(_i)
|
|
|
|
|
|
|
| 168 |
return
|
| 169 |
|
| 170 |
|
| 171 |
@app.cell(hide_code=True)
|
| 172 |
def _(mo):
|
| 173 |
+
mo.md(
|
| 174 |
+
"""
|
| 175 |
+
## Practical loop patterns
|
| 176 |
|
| 177 |
Here are some common patterns you'll use with loops:
|
| 178 |
|
| 179 |
```python
|
| 180 |
# Pattern 1: Accumulator
|
| 181 |
+
value = 0
|
| 182 |
for num in [1, 2, 3, 4, 5]:
|
| 183 |
+
value += num
|
| 184 |
|
| 185 |
# Pattern 2: Search
|
| 186 |
found = False
|
|
|
|
| 195 |
if condition:
|
| 196 |
filtered.append(item)
|
| 197 |
```
|
| 198 |
+
"""
|
| 199 |
+
)
|
| 200 |
return
|
| 201 |
|
| 202 |
|
| 203 |
@app.cell(hide_code=True)
|
| 204 |
def _(mo):
|
| 205 |
+
mo.md(
|
| 206 |
+
r"""
|
| 207 |
+
## Next steps
|
|
|
|
| 208 |
|
| 209 |
+
Check out the official [Python docs on loops and control flow](https://docs.python.org/3/tutorial/controlflow.html).
|
| 210 |
+
"""
|
| 211 |
+
)
|
| 212 |
+
return
|
| 213 |
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
@app.cell
|
| 216 |
+
def _():
|
| 217 |
+
import marimo as mo
|
| 218 |
+
return (mo,)
|
| 219 |
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
Python/006_dictionaries.py
CHANGED
|
@@ -7,16 +7,10 @@
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
-
__generated_with = "0.10.
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
| 14 |
-
@app.cell
|
| 15 |
-
def _():
|
| 16 |
-
import marimo as mo
|
| 17 |
-
return (mo,)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
@app.cell(hide_code=True)
|
| 21 |
def _(mo):
|
| 22 |
mo.md(
|
|
@@ -47,7 +41,7 @@ def _():
|
|
| 47 |
"type": "programming language",
|
| 48 |
"year": 1991,
|
| 49 |
"creator": "Guido van Rossum",
|
| 50 |
-
"is_awesome": True
|
| 51 |
}
|
| 52 |
return (sample_dict,)
|
| 53 |
|
|
@@ -71,6 +65,8 @@ def _(sample_dict):
|
|
| 71 |
def access_dict():
|
| 72 |
print(f"Name: {sample_dict['name']}")
|
| 73 |
print(f"Year: {sample_dict['year']}")
|
|
|
|
|
|
|
| 74 |
access_dict()
|
| 75 |
return (access_dict,)
|
| 76 |
|
|
@@ -81,17 +77,21 @@ def _(sample_dict):
|
|
| 81 |
def safe_access():
|
| 82 |
print(f"Version: {sample_dict.get('version', 'Not specified')}")
|
| 83 |
print(f"Type: {sample_dict.get('type', 'Unknown')}")
|
|
|
|
|
|
|
| 84 |
safe_access()
|
| 85 |
return (safe_access,)
|
| 86 |
|
| 87 |
|
| 88 |
@app.cell(hide_code=True)
|
| 89 |
def _(mo):
|
| 90 |
-
mo.md(
|
|
|
|
| 91 |
## Dictionary Methods
|
| 92 |
|
| 93 |
Python dictionaries come with powerful built-in methods:
|
| 94 |
-
"""
|
|
|
|
| 95 |
return
|
| 96 |
|
| 97 |
|
|
@@ -102,6 +102,8 @@ def _(sample_dict):
|
|
| 102 |
print("Keys:", list(sample_dict.keys()))
|
| 103 |
print("Values:", list(sample_dict.values()))
|
| 104 |
print("Items:", list(sample_dict.items()))
|
|
|
|
|
|
|
| 105 |
view_components()
|
| 106 |
return (view_components,)
|
| 107 |
|
|
@@ -120,17 +122,21 @@ def _():
|
|
| 120 |
# Removing
|
| 121 |
_removed = _dict.pop("b")
|
| 122 |
print(f"Removed {_removed}, Now:", _dict)
|
|
|
|
|
|
|
| 123 |
demonstrate_modification()
|
| 124 |
return (demonstrate_modification,)
|
| 125 |
|
| 126 |
|
| 127 |
@app.cell(hide_code=True)
|
| 128 |
def _(mo):
|
| 129 |
-
mo.md(
|
|
|
|
| 130 |
## Dictionary Comprehension
|
| 131 |
|
| 132 |
Create dictionaries efficiently with dictionary comprehensions:
|
| 133 |
-
"""
|
|
|
|
| 134 |
return
|
| 135 |
|
| 136 |
|
|
@@ -145,17 +151,21 @@ def _():
|
|
| 145 |
# Filtered dictionary
|
| 146 |
_even_squares = {x: x**2 for x in range(5) if x % 2 == 0}
|
| 147 |
print("Even squares:", _even_squares)
|
|
|
|
|
|
|
| 148 |
demonstrate_comprehension()
|
| 149 |
return (demonstrate_comprehension,)
|
| 150 |
|
| 151 |
|
| 152 |
@app.cell(hide_code=True)
|
| 153 |
def _(mo):
|
| 154 |
-
mo.md(
|
|
|
|
| 155 |
## Nested Dictionaries
|
| 156 |
|
| 157 |
Dictionaries can contain other dictionaries, creating complex data structures:
|
| 158 |
-
"""
|
|
|
|
| 159 |
return
|
| 160 |
|
| 161 |
|
|
@@ -166,13 +176,13 @@ def _():
|
|
| 166 |
"alice": {
|
| 167 |
"age": 25,
|
| 168 |
"email": "alice@example.com",
|
| 169 |
-
"interests": ["python", "data science"]
|
| 170 |
},
|
| 171 |
"bob": {
|
| 172 |
"age": 30,
|
| 173 |
"email": "bob@example.com",
|
| 174 |
-
"interests": ["web dev", "gaming"]
|
| 175 |
-
}
|
| 176 |
}
|
| 177 |
}
|
| 178 |
return (nested_data,)
|
|
@@ -196,14 +206,22 @@ def _(nested_data):
|
|
| 196 |
return _current
|
| 197 |
|
| 198 |
print("\nSafe access example:")
|
| 199 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
access_nested()
|
| 201 |
return (access_nested,)
|
| 202 |
|
| 203 |
|
| 204 |
@app.cell(hide_code=True)
|
| 205 |
def _(mo):
|
| 206 |
-
mo.md(
|
|
|
|
| 207 |
## Common Dictionary Patterns
|
| 208 |
|
| 209 |
Here are some useful patterns when working with dictionaries:
|
|
@@ -227,7 +245,8 @@ def _(mo):
|
|
| 227 |
cache[arg] = compute_result(arg)
|
| 228 |
return cache[arg]
|
| 229 |
```
|
| 230 |
-
"""
|
|
|
|
| 231 |
return
|
| 232 |
|
| 233 |
|
|
@@ -250,5 +269,11 @@ def _(mo):
|
|
| 250 |
return (callout_text,)
|
| 251 |
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
if __name__ == "__main__":
|
| 254 |
app.run()
|
|
|
|
| 7 |
|
| 8 |
import marimo
|
| 9 |
|
| 10 |
+
__generated_with = "0.10.19"
|
| 11 |
app = marimo.App()
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.cell(hide_code=True)
|
| 15 |
def _(mo):
|
| 16 |
mo.md(
|
|
|
|
| 41 |
"type": "programming language",
|
| 42 |
"year": 1991,
|
| 43 |
"creator": "Guido van Rossum",
|
| 44 |
+
"is_awesome": True,
|
| 45 |
}
|
| 46 |
return (sample_dict,)
|
| 47 |
|
|
|
|
| 65 |
def access_dict():
|
| 66 |
print(f"Name: {sample_dict['name']}")
|
| 67 |
print(f"Year: {sample_dict['year']}")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
access_dict()
|
| 71 |
return (access_dict,)
|
| 72 |
|
|
|
|
| 77 |
def safe_access():
|
| 78 |
print(f"Version: {sample_dict.get('version', 'Not specified')}")
|
| 79 |
print(f"Type: {sample_dict.get('type', 'Unknown')}")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
safe_access()
|
| 83 |
return (safe_access,)
|
| 84 |
|
| 85 |
|
| 86 |
@app.cell(hide_code=True)
|
| 87 |
def _(mo):
|
| 88 |
+
mo.md(
|
| 89 |
+
"""
|
| 90 |
## Dictionary Methods
|
| 91 |
|
| 92 |
Python dictionaries come with powerful built-in methods:
|
| 93 |
+
"""
|
| 94 |
+
)
|
| 95 |
return
|
| 96 |
|
| 97 |
|
|
|
|
| 102 |
print("Keys:", list(sample_dict.keys()))
|
| 103 |
print("Values:", list(sample_dict.values()))
|
| 104 |
print("Items:", list(sample_dict.items()))
|
| 105 |
+
|
| 106 |
+
|
| 107 |
view_components()
|
| 108 |
return (view_components,)
|
| 109 |
|
|
|
|
| 122 |
# Removing
|
| 123 |
_removed = _dict.pop("b")
|
| 124 |
print(f"Removed {_removed}, Now:", _dict)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
demonstrate_modification()
|
| 128 |
return (demonstrate_modification,)
|
| 129 |
|
| 130 |
|
| 131 |
@app.cell(hide_code=True)
|
| 132 |
def _(mo):
|
| 133 |
+
mo.md(
|
| 134 |
+
"""
|
| 135 |
## Dictionary Comprehension
|
| 136 |
|
| 137 |
Create dictionaries efficiently with dictionary comprehensions:
|
| 138 |
+
"""
|
| 139 |
+
)
|
| 140 |
return
|
| 141 |
|
| 142 |
|
|
|
|
| 151 |
# Filtered dictionary
|
| 152 |
_even_squares = {x: x**2 for x in range(5) if x % 2 == 0}
|
| 153 |
print("Even squares:", _even_squares)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
demonstrate_comprehension()
|
| 157 |
return (demonstrate_comprehension,)
|
| 158 |
|
| 159 |
|
| 160 |
@app.cell(hide_code=True)
|
| 161 |
def _(mo):
|
| 162 |
+
mo.md(
|
| 163 |
+
"""
|
| 164 |
## Nested Dictionaries
|
| 165 |
|
| 166 |
Dictionaries can contain other dictionaries, creating complex data structures:
|
| 167 |
+
"""
|
| 168 |
+
)
|
| 169 |
return
|
| 170 |
|
| 171 |
|
|
|
|
| 176 |
"alice": {
|
| 177 |
"age": 25,
|
| 178 |
"email": "alice@example.com",
|
| 179 |
+
"interests": ["python", "data science"],
|
| 180 |
},
|
| 181 |
"bob": {
|
| 182 |
"age": 30,
|
| 183 |
"email": "bob@example.com",
|
| 184 |
+
"interests": ["web dev", "gaming"],
|
| 185 |
+
},
|
| 186 |
}
|
| 187 |
}
|
| 188 |
return (nested_data,)
|
|
|
|
| 206 |
return _current
|
| 207 |
|
| 208 |
print("\nSafe access example:")
|
| 209 |
+
print(
|
| 210 |
+
"Charlie's age:",
|
| 211 |
+
_get_nested(
|
| 212 |
+
nested_data, "users", "charlie", "age", default="Not found"
|
| 213 |
+
),
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
access_nested()
|
| 218 |
return (access_nested,)
|
| 219 |
|
| 220 |
|
| 221 |
@app.cell(hide_code=True)
|
| 222 |
def _(mo):
|
| 223 |
+
mo.md(
|
| 224 |
+
"""
|
| 225 |
## Common Dictionary Patterns
|
| 226 |
|
| 227 |
Here are some useful patterns when working with dictionaries:
|
|
|
|
| 245 |
cache[arg] = compute_result(arg)
|
| 246 |
return cache[arg]
|
| 247 |
```
|
| 248 |
+
"""
|
| 249 |
+
)
|
| 250 |
return
|
| 251 |
|
| 252 |
|
|
|
|
| 269 |
return (callout_text,)
|
| 270 |
|
| 271 |
|
| 272 |
+
@app.cell
|
| 273 |
+
def _():
|
| 274 |
+
import marimo as mo
|
| 275 |
+
return (mo,)
|
| 276 |
+
|
| 277 |
+
|
| 278 |
if __name__ == "__main__":
|
| 279 |
app.run()
|