File size: 4,632 Bytes
ebb1733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9988d70
 
 
ebb1733
 
9988d70
ebb1733
9988d70
 
 
ebb1733
 
 
9988d70
 
 
 
 
 
ebb1733
9988d70
 
 
 
ebb1733
9988d70
 
 
 
 
ebb1733
 
 
9988d70
ebb1733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9988d70
 
 
 
 
ebb1733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9988d70
ebb1733
 
 
 
 
770a2df
ebb1733
 
 
 
 
 
 
 
 
 
 
 
 
9988d70
 
 
 
 
 
 
 
 
 
ebb1733
 
 
9988d70
ebb1733
9988d70
ebb1733
9988d70
 
ebb1733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "marimo",
# ]
# ///

import marimo

__generated_with = "0.10.16"
app = marimo.App()


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        # 🧩 Function Design in Python

        Dive into the world of Python functions — where code becomes modular and powerful!

        ## Function Basics
        Functions help you:

        - Break down complex problems

        - Create reusable code blocks

        - Improve code readability (good practice)

        ```python
        def function_name(parameters):
            '''Docstring explaining the function'''
            # Function body
            return result
        ```
        """
    )
    return


@app.cell
def _():
    # Example function with parameter
    def greet(name):
        return f"Hello, {name}!"

    name = "Python Learner"
    return greet, name


@app.cell
def _(greet, name):
    greet(name)
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        ## Default Parameters
        Make your functions more flexible by providing default values.
        """
    )
    return


@app.cell
def _():
    # Function with default parameter
    def create_profile(name, age=18):
        return f"{name} is {age} years old"

    # Example usage
    example_name = "Alex"
    example_profile = create_profile(example_name)
    example_profile
    return create_profile, example_name, example_profile


@app.cell
def _():
    # Show closure over values
    base_multiplier = 2
    def multiplier(x):
        """
        Create a function that multiplies input by a base value.

        This demonstrates how functions can 'close over' 
        values from their surrounding scope.
        """
        return x * base_multiplier

    # Example of using the closure
    sample_numbers = [1, 2, 3, 4, 5]
    multiplied_numbers = [multiplier(num) for num in sample_numbers]
    print(multiplied_numbers)
    return base_multiplier, multiplied_numbers, multiplier, sample_numbers


@app.cell
def _():
    def calculate(a, b):
        """
        Perform multiple calculations on two numbers.

        Args:
            a (int): First number
            b (int): Second number

        Returns:
            dict: Results of various calculations
        """
        return {
            "sum": a + b,
            "product": a * b,
            "difference": a - b,
            "max": max(a, b)
        }

    # Example usage with concrete values
    first_number = 10
    second_number = 5
    result = calculate(first_number, second_number)
    return calculate, first_number, result, second_number


@app.cell(hide_code=True)
def _(mo, result):
    mo.md(f"""
    ## Function Results

    Calculation Results:
    {result}
    """)
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        """
        ## Multiple Return Values
        Python allows returning multiple values easily:

        ```python
        def multiple_returns():
            return value1, value2, value3

        # Unpacking returns
        x, y, z = multiple_returns()
        ```
        """
    )
    return


@app.cell
def _():
    def weather_analysis(temp):
        """
        Analyze weather based on temperature.

        Args:
            temp (float): Temperature in Celsius

        Returns:
            tuple: Weather status, recommendation, warning level
        """
        if temp <= 0:
            return "Freezing", "Wear heavy coat", "High"
        elif 0 < temp <= 15:
            return "Cold", "Layer up", "Medium"
        elif 15 < temp <= 25:
            return "Mild", "Comfortable clothing", "Low"
        else:
            return "Hot", "Stay hydrated", "High"

    # Example temperature analysis
    temperature = 25
    status, recommendation, warning_level = weather_analysis(temperature)
    return (
        recommendation,
        status,
        temperature,
        warning_level,
        weather_analysis,
    )


@app.cell(hide_code=True)
def _(mo, recommendation, status, warning_level):
    mo.md(f"""
    ## Function Results

    Calculation Results:
    {status}, {recommendation}, {warning_level}
    """)
    return


@app.cell(hide_code=True)
def _(mo):
    callout_text = mo.md("""
    ## Your Function Design Journey!

    Next Steps:

    - Practice creating functions

    - Experiment with default parameters

    - Explore multiple return values

    """)

    mo.callout(callout_text, kind="success")
    return (callout_text,)


if __name__ == "__main__":
    app.run()