Spaces:
Running
Running
| # /// script | |
| # requires-python = ">=3.10" | |
| # dependencies = [ | |
| # "marimo", | |
| # ] | |
| # /// | |
| import marimo | |
| __generated_with = "0.10.14" | |
| app = marimo.App() | |
| def _(): | |
| import marimo as mo | |
| return (mo,) | |
| def _(mo): | |
| mo.md( | |
| """ | |
| # π Conditional Logic in Python | |
| Learn how to make decisions in your code with Python's conditional statements! | |
| ## If Statements | |
| The foundation of decision-making in Python: | |
| ```python | |
| if condition: | |
| # code to run if condition is True | |
| elif another_condition: | |
| # code to run if another_condition is True | |
| else: | |
| # code to run if no conditions are True | |
| ``` | |
| Let's explore with some examples: | |
| """ | |
| ) | |
| return | |
| def _(): | |
| number = 42 | |
| return (number,) | |
| def _(number): | |
| if number > 50: | |
| result = "Greater than 50" | |
| elif number == 42: | |
| result = "Found the answer!" | |
| else: | |
| result = "Less than 50" | |
| result | |
| return (result,) | |
| def _(mo): | |
| mo.md( | |
| r""" | |
| ## Interactive Decision Making | |
| Try changing the conditions below and see how the results change (powered by marimo's UI elements!): | |
| """ | |
| ) | |
| return | |
| def _(mo, threshold, value): | |
| mo.hstack([value, threshold]) | |
| return | |
| def _(mo): | |
| value = mo.ui.number(value=25, start=0, stop=100, label="Enter a number") | |
| threshold = mo.ui.slider(value=50, start=0, stop=100, label="Set threshold") | |
| return threshold, value | |
| def _(mo, threshold, value): | |
| if value.value > threshold.value: | |
| decision = f"{value.value} is greater than {threshold.value}" | |
| elif value.value == threshold.value: | |
| decision = f"{value.value} is equal to {threshold.value}" | |
| else: | |
| decision = f"{value.value} is less than {threshold.value}" | |
| mo.hstack( | |
| [ | |
| mo.md(f"**Decision**: {decision}"), | |
| mo.md(f"**Condition Status**: {'β ' if value.value >= threshold.value else 'β'}") | |
| ], | |
| justify="space-between" | |
| ) | |
| return (decision,) | |
| def _(mo): | |
| mo.md( | |
| r""" | |
| ## Boolean Operations | |
| Python uses boolean operators to combine conditions: | |
| - `and`: Both conditions must be True | |
| - `or`: At least one condition must be True | |
| - `not`: Inverts the condition | |
| """ | |
| ) | |
| return | |
| def _(age, has_id, mo): | |
| mo.hstack([age, has_id]) | |
| return | |
| def _(mo): | |
| age = mo.ui.number(value=18, start=0, stop=120, label="Age") | |
| has_id = mo.ui.switch(value=True, label="Has ID") | |
| return age, has_id | |
| def _(age, has_id, mo): | |
| can_vote = age.value >= 18 and has_id.value | |
| explanation = f""" | |
| ### Voting Eligibility Check | |
| Current Status: | |
| - Age: {age.value} years old | |
| - Has ID: {'Yes' if has_id.value else 'No'} | |
| - Can Vote: {'Yes β ' if can_vote else 'No β'} | |
| Reason: {'Both age and ID requirements met' if can_vote | |
| else 'Missing ' + ('required age' if age.value < 18 else 'valid ID')} | |
| """ | |
| mo.md(explanation) | |
| return can_vote, explanation | |
| def _(mo): | |
| _text = mo.md(""" | |
| - Try different combinations of age and ID status | |
| - Notice how both conditions must be True to allow voting | |
| - Experiment with edge cases (exactly 18, no ID, etc.) | |
| """) | |
| mo.accordion({"π‘ Experiment Tips": _text}) | |
| return | |
| def _(mo): | |
| mo.md( | |
| """ | |
| ## Complex Conditions | |
| Combine multiple conditions for more sophisticated logic: | |
| ```python | |
| # Multiple conditions | |
| if (age >= 18 and has_id) or has_special_permission: | |
| print("Access granted") | |
| # Nested conditions | |
| if age >= 18: | |
| if has_id: | |
| print("Full access") | |
| else: | |
| print("Limited access") | |
| ``` | |
| """ | |
| ) | |
| return | |
| def _(humidity, mo, temp, wind): | |
| mo.hstack([temp, humidity, wind]) | |
| return | |
| def _(mo): | |
| temp = mo.ui.number(value=25, start=-20, stop=50, label="Temperature (Β°C)") | |
| humidity = mo.ui.slider(value=60, start=0, stop=100, label="Humidity (%)") | |
| wind = mo.ui.number(value=10, start=0, stop=100, label="Wind Speed (km/h)") | |
| return humidity, temp, wind | |
| def _(humidity, mo, temp, wind): | |
| def get_weather_advice(): | |
| conditions = [] | |
| if temp.value > 30: | |
| conditions.append("π‘οΈ High temperature") | |
| elif temp.value < 10: | |
| conditions.append("βοΈ Cold temperature") | |
| if humidity.value > 80: | |
| conditions.append("π§ High humidity") | |
| elif humidity.value < 30: | |
| conditions.append("ποΈ Low humidity") | |
| if wind.value > 30: | |
| conditions.append("π¨ Strong winds") | |
| return conditions | |
| conditions = get_weather_advice() | |
| message = f""" | |
| ### Weather Analysis | |
| Current Conditions: | |
| - Temperature: {temp.value}Β°C | |
| - Humidity: {humidity.value}% | |
| - Wind Speed: {wind.value} km/h | |
| Alerts: {', '.join(conditions) if conditions else 'No special alerts'} | |
| """ | |
| mo.md(message) | |
| return conditions, get_weather_advice, message | |
| def _(mo): | |
| callout_text = mo.md(""" | |
| ## Your Logic Journey Continues! | |
| Next Steps: | |
| - Practice combining multiple conditions | |
| - Explore nested if statements | |
| - Try creating your own complex decision trees (pun on an ML algorithm!) | |
| Keep coding! π―β¨ | |
| """) | |
| mo.callout(callout_text, kind="success") | |
| return (callout_text,) | |
| if __name__ == "__main__": | |
| app.run() | |