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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.14"
|
| 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 |
+
Learn how to make decisions in your code with Python's conditional statements!
|
| 27 |
+
|
| 28 |
+
## If Statements
|
| 29 |
+
The foundation of decision-making in Python:
|
| 30 |
+
```python
|
| 31 |
+
if condition:
|
| 32 |
+
# code to run if condition is True
|
| 33 |
+
elif another_condition:
|
| 34 |
+
# code to run if another_condition is True
|
| 35 |
+
else:
|
| 36 |
+
# code to run if no conditions are True
|
| 37 |
+
```
|
| 38 |
+
Let's explore with some examples:
|
| 39 |
+
"""
|
| 40 |
+
)
|
| 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 > 50:
|
| 53 |
+
result = "Greater than 50"
|
| 54 |
+
elif number == 42:
|
| 55 |
+
result = "Found the answer!"
|
| 56 |
+
else:
|
| 57 |
+
result = "Less than 50"
|
| 58 |
+
result
|
| 59 |
+
return (result,)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.cell(hide_code=True)
|
| 63 |
+
def _(mo):
|
| 64 |
+
mo.md(
|
| 65 |
+
r"""
|
| 66 |
+
## Interactive Decision Making
|
| 67 |
+
Try changing the conditions below and see how the results change (powered by marimo's UI elements!):
|
| 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}"
|
| 90 |
+
elif value.value == threshold.value:
|
| 91 |
+
decision = f"{value.value} is equal to {threshold.value}"
|
| 92 |
+
else:
|
| 93 |
+
decision = f"{value.value} is less than {threshold.value}"
|
| 94 |
+
|
| 95 |
+
mo.hstack(
|
| 96 |
+
[
|
| 97 |
+
mo.md(f"**Decision**: {decision}"),
|
| 98 |
+
mo.md(f"**Condition Status**: {'✅' if value.value >= threshold.value else '❌'}")
|
| 99 |
+
],
|
| 100 |
+
justify="space-between"
|
| 101 |
+
)
|
| 102 |
+
return (decision,)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@app.cell(hide_code=True)
|
| 106 |
+
def _(mo):
|
| 107 |
+
mo.md(
|
| 108 |
+
r"""
|
| 109 |
+
## Boolean Operations
|
| 110 |
+
Python uses boolean operators to combine conditions:
|
| 111 |
+
|
| 112 |
+
- `and`: Both conditions must be True
|
| 113 |
+
|
| 114 |
+
- `or`: At least one condition must be True
|
| 115 |
+
|
| 116 |
+
- `not`: Inverts the condition
|
| 117 |
+
"""
|
| 118 |
+
)
|
| 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 |
+
|
| 139 |
+
explanation = f"""
|
| 140 |
+
### Voting Eligibility Check
|
| 141 |
+
|
| 142 |
+
Current Status:
|
| 143 |
+
|
| 144 |
+
- Age: {age.value} years old
|
| 145 |
+
|
| 146 |
+
- Has ID: {'Yes' if has_id.value else 'No'}
|
| 147 |
+
|
| 148 |
+
- Can Vote: {'Yes ✅' if can_vote else 'No ❌'}
|
| 149 |
+
|
| 150 |
+
Reason: {'Both age and ID requirements met' if can_vote
|
| 151 |
+
else 'Missing ' + ('required age' if age.value < 18 else 'valid ID')}
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
mo.md(explanation)
|
| 155 |
+
return can_vote, explanation
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
@app.cell(hide_code=True)
|
| 159 |
+
def _(mo):
|
| 160 |
+
_text = mo.md("""
|
| 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(
|
| 172 |
+
"""
|
| 173 |
+
## Complex Conditions
|
| 174 |
+
Combine multiple conditions for more sophisticated logic:
|
| 175 |
+
```python
|
| 176 |
+
# Multiple conditions
|
| 177 |
+
if (age >= 18 and has_id) or has_special_permission:
|
| 178 |
+
print("Access granted")
|
| 179 |
+
|
| 180 |
+
# Nested conditions
|
| 181 |
+
if age >= 18:
|
| 182 |
+
if has_id:
|
| 183 |
+
print("Full access")
|
| 184 |
+
else:
|
| 185 |
+
print("Limited access")
|
| 186 |
+
```
|
| 187 |
+
"""
|
| 188 |
+
)
|
| 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 (%)")
|
| 202 |
+
wind = mo.ui.number(value=10, start=0, stop=100, label="Wind Speed (km/h)")
|
| 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 |
+
|
| 231 |
+
Current Conditions:
|
| 232 |
+
|
| 233 |
+
- Temperature: {temp.value}°C
|
| 234 |
+
|
| 235 |
+
- Humidity: {humidity.value}%
|
| 236 |
+
|
| 237 |
+
- Wind Speed: {wind.value} km/h
|
| 238 |
+
|
| 239 |
+
Alerts: {', '.join(conditions) if conditions else 'No special alerts'}
|
| 240 |
+
"""
|
| 241 |
+
|
| 242 |
+
mo.md(message)
|
| 243 |
+
return conditions, get_weather_advice, message
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
@app.cell(hide_code=True)
|
| 247 |
+
def _(mo):
|
| 248 |
+
callout_text = mo.md("""
|
| 249 |
+
## Your Logic Journey Continues!
|
| 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 |
+
mo.callout(callout_text, kind="success")
|
| 263 |
+
return (callout_text,)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
if __name__ == "__main__":
|
| 267 |
+
app.run()
|