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# 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 _():
|
77 |
+
# Function with default parameter
|
78 |
+
def create_profile(name, age=18):
|
79 |
+
return f"{name} is {age} years old"
|
80 |
+
|
81 |
+
# Example usage
|
82 |
+
example_name = "Alex"
|
83 |
+
example_profile = create_profile(example_name)
|
84 |
+
example_profile
|
85 |
+
return create_profile, example_name, example_profile
|
86 |
+
|
87 |
+
|
88 |
+
@app.cell
|
89 |
+
def _():
|
90 |
+
# Show closure over values
|
91 |
+
base_multiplier = 2
|
92 |
+
def multiplier(x):
|
93 |
+
"""
|
94 |
+
Create a function that multiplies input by a base value.
|
95 |
+
|
96 |
+
This demonstrates how functions can 'close over'
|
97 |
+
values from their surrounding scope.
|
98 |
+
"""
|
99 |
+
return x * base_multiplier
|
100 |
+
|
101 |
+
# Example of using the closure
|
102 |
+
sample_numbers = [1, 2, 3, 4, 5]
|
103 |
+
multiplied_numbers = [multiplier(num) for num in sample_numbers]
|
104 |
+
print(multiplied_numbers)
|
105 |
+
return base_multiplier, multiplied_numbers, multiplier, sample_numbers
|
106 |
+
|
107 |
+
|
108 |
+
@app.cell
|
109 |
+
def _():
|
110 |
+
def calculate(a, b):
|
111 |
+
"""
|
112 |
+
Perform multiple calculations on two numbers.
|
113 |
+
|
114 |
+
Args:
|
115 |
+
a (int): First number
|
116 |
+
b (int): Second number
|
117 |
+
|
118 |
+
Returns:
|
119 |
+
dict: Results of various calculations
|
120 |
+
"""
|
121 |
+
return {
|
122 |
+
"sum": a + b,
|
123 |
+
"product": a * b,
|
124 |
+
"difference": a - b,
|
125 |
+
"max": max(a, b)
|
126 |
+
}
|
127 |
+
|
128 |
+
# Example usage with concrete values
|
129 |
+
first_number = 10
|
130 |
+
second_number = 5
|
131 |
+
result = calculate(first_number, second_number)
|
132 |
+
return calculate, first_number, result, second_number
|
133 |
+
|
134 |
+
|
135 |
+
@app.cell(hide_code=True)
|
136 |
+
def _(mo, result):
|
137 |
+
mo.md(f"""
|
138 |
+
## Function Results
|
139 |
+
|
140 |
+
Calculation Results:
|
141 |
+
{result}
|
142 |
+
""")
|
143 |
+
return
|
144 |
+
|
145 |
+
|
146 |
+
@app.cell(hide_code=True)
|
147 |
+
def _(mo):
|
148 |
+
mo.md(
|
149 |
+
"""
|
150 |
+
## Multiple Return Values
|
151 |
+
Python allows returning multiple values easily:
|
152 |
+
|
153 |
+
```python
|
154 |
+
def multiple_returns():
|
155 |
+
return value1, value2, value3
|
156 |
+
|
157 |
+
# Unpacking returns
|
158 |
+
x, y, z = multiple_returns()
|
159 |
+
```
|
160 |
+
"""
|
161 |
+
)
|
162 |
+
return
|
163 |
+
|
164 |
+
|
165 |
+
@app.cell
|
166 |
+
def _():
|
167 |
+
def weather_analysis(temp):
|
168 |
+
"""
|
169 |
+
Analyze weather based on temperature.
|
170 |
+
|
171 |
+
Args:
|
172 |
+
temp (float): Temperature in Celsius
|
173 |
+
|
174 |
+
Returns:
|
175 |
+
tuple: Weather status, recommendation, warning level
|
176 |
+
"""
|
177 |
+
if temp <= 0:
|
178 |
+
return "Freezing", "Wear heavy coat", "High"
|
179 |
+
elif 0 < temp <= 15:
|
180 |
+
return "Cold", "Layer up", "Medium"
|
181 |
+
elif 15 < temp <= 25:
|
182 |
+
return "Mild", "Comfortable clothing", "Low"
|
183 |
+
else:
|
184 |
+
return "Hot", "Stay hydrated", "High"
|
185 |
+
|
186 |
+
# Example temperature analysis
|
187 |
+
temperature = 25
|
188 |
+
status, recommendation, warning_level = weather_analysis(temperature)
|
189 |
+
return (
|
190 |
+
recommendation,
|
191 |
+
status,
|
192 |
+
temperature,
|
193 |
+
warning_level,
|
194 |
+
weather_analysis,
|
195 |
+
)
|
196 |
+
|
197 |
+
|
198 |
+
@app.cell(hide_code=True)
|
199 |
+
def _(mo, recommendation, status, warning_level):
|
200 |
+
mo.md(f"""
|
201 |
+
## Function Results
|
202 |
+
|
203 |
+
Calculation Results:
|
204 |
+
{status}, {recommendation}, {warning_level}
|
205 |
+
""")
|
206 |
+
return
|
207 |
+
|
208 |
+
|
209 |
+
@app.cell(hide_code=True)
|
210 |
+
def _(mo):
|
211 |
+
callout_text = mo.md("""
|
212 |
+
## Your Function Design Journey!
|
213 |
+
|
214 |
+
Next Steps:
|
215 |
+
|
216 |
+
- Practice creating functions
|
217 |
+
|
218 |
+
- Experiment with default parameters
|
219 |
+
|
220 |
+
- Explore multiple return values
|
221 |
+
|
222 |
+
""")
|
223 |
+
|
224 |
+
mo.callout(callout_text, kind="success")
|
225 |
+
return (callout_text,)
|
226 |
+
|
227 |
+
|
228 |
+
if __name__ == "__main__":
|
229 |
+
app.run()
|