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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "marimo",
|
| 5 |
+
# ]
|
| 6 |
+
# ///
|
| 7 |
+
|
| 8 |
+
import marimo
|
| 9 |
+
|
| 10 |
+
__generated_with = "0.10.16"
|
| 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 |
+
# 🧩 Function Design in Python
|
| 25 |
+
|
| 26 |
+
Dive into the world of Python functions — where code becomes modular and powerful!
|
| 27 |
+
|
| 28 |
+
## Function Basics
|
| 29 |
+
Functions help you:
|
| 30 |
+
|
| 31 |
+
- Break down complex problems
|
| 32 |
+
|
| 33 |
+
- Create reusable code blocks
|
| 34 |
+
|
| 35 |
+
- Improve code readability (good practice)
|
| 36 |
+
|
| 37 |
+
```python
|
| 38 |
+
def function_name(parameters):
|
| 39 |
+
'''Docstring explaining the function'''
|
| 40 |
+
# Function body
|
| 41 |
+
return result
|
| 42 |
+
```
|
| 43 |
+
"""
|
| 44 |
+
)
|
| 45 |
+
return
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@app.cell
|
| 49 |
+
def _():
|
| 50 |
+
# Example function with parameter
|
| 51 |
+
def greet(name):
|
| 52 |
+
return f"Hello, {name}!"
|
| 53 |
+
|
| 54 |
+
name = "Python Learner"
|
| 55 |
+
return greet, name
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@app.cell
|
| 59 |
+
def _(greet, name):
|
| 60 |
+
greet(name)
|
| 61 |
+
return
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@app.cell(hide_code=True)
|
| 65 |
+
def _(mo):
|
| 66 |
+
mo.md(
|
| 67 |
+
"""
|
| 68 |
+
## Default Parameters
|
| 69 |
+
Make your functions more flexible by providing default values.
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@app.cell
|
| 76 |
+
def _(default_age):
|
| 77 |
+
default_age
|
| 78 |
+
return
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@app.cell
|
| 82 |
+
def _(mo):
|
| 83 |
+
# Interactive function (default parameter demo)
|
| 84 |
+
default_age = mo.ui.number(value=18, start=0, stop=120, label="Default Age")
|
| 85 |
+
return (default_age,)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@app.cell
|
| 89 |
+
def _(default_age):
|
| 90 |
+
def create_profile(name, age=default_age.value):
|
| 91 |
+
return f"{name} is {age} years old"
|
| 92 |
+
|
| 93 |
+
example_name = "Alex"
|
| 94 |
+
return create_profile, example_name
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@app.cell
|
| 98 |
+
def _(create_profile, example_name):
|
| 99 |
+
create_profile(example_name)
|
| 100 |
+
return
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@app.cell
|
| 104 |
+
def _(first_param, mo, second_param):
|
| 105 |
+
mo.hstack([first_param, second_param])
|
| 106 |
+
return
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@app.cell
|
| 110 |
+
def _(mo):
|
| 111 |
+
# Multiple parameters interactive function demo
|
| 112 |
+
first_param = mo.ui.number(value=10, start=0, stop=100, label="First Number")
|
| 113 |
+
second_param = mo.ui.number(value=5, start=0, stop=100, label="Second Number")
|
| 114 |
+
return first_param, second_param
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@app.cell
|
| 118 |
+
def _(first_param, second_param):
|
| 119 |
+
def calculate(a, b):
|
| 120 |
+
"""
|
| 121 |
+
Perform multiple calculations on two numbers.
|
| 122 |
+
|
| 123 |
+
Args:
|
| 124 |
+
a (int): First number
|
| 125 |
+
b (int): Second number
|
| 126 |
+
|
| 127 |
+
Returns:
|
| 128 |
+
dict: Results of various calculations
|
| 129 |
+
"""
|
| 130 |
+
return {
|
| 131 |
+
"sum": a + b,
|
| 132 |
+
"product": a * b,
|
| 133 |
+
"difference": a - b,
|
| 134 |
+
"max": max(a, b)
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
result = calculate(first_param.value, second_param.value)
|
| 138 |
+
return calculate, result
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
@app.cell(hide_code=True)
|
| 142 |
+
def _(mo, result):
|
| 143 |
+
mo.md(f"""
|
| 144 |
+
## Function Results
|
| 145 |
+
|
| 146 |
+
Calculation Results:
|
| 147 |
+
{result}
|
| 148 |
+
""")
|
| 149 |
+
return
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@app.cell(hide_code=True)
|
| 153 |
+
def _(mo):
|
| 154 |
+
mo.md(
|
| 155 |
+
"""
|
| 156 |
+
## Multiple Return Values
|
| 157 |
+
Python allows returning multiple values easily:
|
| 158 |
+
|
| 159 |
+
```python
|
| 160 |
+
def multiple_returns():
|
| 161 |
+
return value1, value2, value3
|
| 162 |
+
|
| 163 |
+
# Unpacking returns
|
| 164 |
+
x, y, z = multiple_returns()
|
| 165 |
+
```
|
| 166 |
+
"""
|
| 167 |
+
)
|
| 168 |
+
return
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
@app.cell
|
| 172 |
+
def _(temperature):
|
| 173 |
+
temperature
|
| 174 |
+
return
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@app.cell
|
| 178 |
+
def _(mo):
|
| 179 |
+
# Multiple return values and how they are captured
|
| 180 |
+
temperature = mo.ui.number(value=25, start=-50, stop=50, label="Temperature")
|
| 181 |
+
return (temperature,)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
@app.cell
|
| 185 |
+
def _(temperature):
|
| 186 |
+
def weather_analysis(temp):
|
| 187 |
+
"""
|
| 188 |
+
Analyze weather based on temperature.
|
| 189 |
+
|
| 190 |
+
Args:
|
| 191 |
+
temp (float): Temperature in Celsius (superior unit of measurement)
|
| 192 |
+
|
| 193 |
+
Returns:
|
| 194 |
+
tuple: Weather status, recommendation, warning level
|
| 195 |
+
"""
|
| 196 |
+
if temp <= 0:
|
| 197 |
+
return "Freezing", "Wear heavy coat", "High"
|
| 198 |
+
elif 0 < temp <= 15:
|
| 199 |
+
return "Cold", "Layer up", "Medium"
|
| 200 |
+
elif 15 < temp <= 25:
|
| 201 |
+
return "Mild", "Comfortable clothing", "Low"
|
| 202 |
+
else:
|
| 203 |
+
return "Hot", "Stay hydrated", "High"
|
| 204 |
+
|
| 205 |
+
analysis = weather_analysis(temperature.value)
|
| 206 |
+
return analysis, weather_analysis
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
@app.cell(hide_code=True)
|
| 210 |
+
def _(mo, weather_analysis):
|
| 211 |
+
mo.md(f"""
|
| 212 |
+
## Multiple Return Demonstration
|
| 213 |
+
|
| 214 |
+
Current Temperature Analysis:
|
| 215 |
+
{weather_analysis(25)}
|
| 216 |
+
""")
|
| 217 |
+
return
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
@app.cell(hide_code=True)
|
| 221 |
+
def _(mo):
|
| 222 |
+
callout_text = mo.md("""
|
| 223 |
+
## Your Function Design Journey!
|
| 224 |
+
|
| 225 |
+
Next Steps:
|
| 226 |
+
|
| 227 |
+
- Practice creating functions
|
| 228 |
+
|
| 229 |
+
- Experiment with default parameters
|
| 230 |
+
|
| 231 |
+
- Explore multiple return values
|
| 232 |
+
|
| 233 |
+
""")
|
| 234 |
+
|
| 235 |
+
mo.callout(callout_text, kind="success")
|
| 236 |
+
return (callout_text,)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
if __name__ == "__main__":
|
| 240 |
+
app.run()
|