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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 |
+
# 🔄 Loops in Python
|
| 25 |
+
|
| 26 |
+
Let's explore how Python helps us repeat tasks efficiently with loops!
|
| 27 |
+
|
| 28 |
+
## Types of Loops
|
| 29 |
+
Python has two main types of loops:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
# For loop - when you know how many times to repeat
|
| 33 |
+
for i in range(5):
|
| 34 |
+
print(i)
|
| 35 |
+
|
| 36 |
+
# While loop - when you don't know how many repetitions
|
| 37 |
+
while condition:
|
| 38 |
+
do_something()
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
Let's start with a simple list to explore loops.
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@app.cell
|
| 48 |
+
def _():
|
| 49 |
+
sample_fruits = ["apple", "banana", "orange", "grape"]
|
| 50 |
+
return (sample_fruits,)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@app.cell(hide_code=True)
|
| 54 |
+
def _(mo):
|
| 55 |
+
mo.md(
|
| 56 |
+
"""
|
| 57 |
+
## For Loop Basics
|
| 58 |
+
|
| 59 |
+
The for loop is perfect for iterating over sequences.
|
| 60 |
+
Try changing the `sample_fruits` list above and see how the output changes.
|
| 61 |
+
"""
|
| 62 |
+
)
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@app.cell
|
| 67 |
+
def _(sample_fruits):
|
| 68 |
+
def _print_fruits():
|
| 69 |
+
for _fruit in sample_fruits:
|
| 70 |
+
print(f"I like {_fruit}s!")
|
| 71 |
+
_print_fruits()
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@app.cell(hide_code=True)
|
| 76 |
+
def _(mo):
|
| 77 |
+
mo.md("""
|
| 78 |
+
## Using enumerate()
|
| 79 |
+
|
| 80 |
+
When you need both the item and its position, use enumerate():
|
| 81 |
+
""")
|
| 82 |
+
return
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
@app.cell
|
| 86 |
+
def _(sample_fruits):
|
| 87 |
+
def _print_enumerated():
|
| 88 |
+
for _idx, _fruit in enumerate(sample_fruits):
|
| 89 |
+
print(f"{_idx + 1}. {_fruit}")
|
| 90 |
+
_print_enumerated()
|
| 91 |
+
return
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@app.cell(hide_code=True)
|
| 95 |
+
def _(mo):
|
| 96 |
+
mo.md("""
|
| 97 |
+
## Range in Loops
|
| 98 |
+
|
| 99 |
+
range() is a powerful function for generating sequences of numbers:
|
| 100 |
+
""")
|
| 101 |
+
return
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
@app.cell
|
| 105 |
+
def _():
|
| 106 |
+
def _demonstrate_range():
|
| 107 |
+
print("range(5):", list(range(5)))
|
| 108 |
+
print("range(2, 5):", list(range(2, 5)))
|
| 109 |
+
print("range(0, 10, 2):", list(range(0, 10, 2)))
|
| 110 |
+
_demonstrate_range()
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@app.cell(hide_code=True)
|
| 115 |
+
def _(mo):
|
| 116 |
+
mo.md("""## While Loop Basics
|
| 117 |
+
|
| 118 |
+
While loops continue as long as a condition is `True`.""")
|
| 119 |
+
return
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
@app.cell
|
| 123 |
+
def _():
|
| 124 |
+
def _count_up():
|
| 125 |
+
_count = 0
|
| 126 |
+
while _count < 5:
|
| 127 |
+
print(f"Count is {_count}")
|
| 128 |
+
_count += 1
|
| 129 |
+
_count_up()
|
| 130 |
+
return
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@app.cell(hide_code=True)
|
| 134 |
+
def _(mo):
|
| 135 |
+
mo.md("""
|
| 136 |
+
## Loop Control Statements
|
| 137 |
+
|
| 138 |
+
Python provides several ways to control loop execution:
|
| 139 |
+
|
| 140 |
+
- `break`: Exit the loop immediately
|
| 141 |
+
|
| 142 |
+
- `continue`: Skip to the next iteration
|
| 143 |
+
|
| 144 |
+
- `else`: Execute when loop completes normally
|
| 145 |
+
""")
|
| 146 |
+
return
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
@app.cell
|
| 150 |
+
def _():
|
| 151 |
+
def _demonstrate_break():
|
| 152 |
+
for _i in range(1, 6):
|
| 153 |
+
if _i == 4:
|
| 154 |
+
break
|
| 155 |
+
print(_i)
|
| 156 |
+
print("Loop ended early!")
|
| 157 |
+
_demonstrate_break()
|
| 158 |
+
return
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
@app.cell
|
| 162 |
+
def _():
|
| 163 |
+
def _demonstrate_continue():
|
| 164 |
+
for _i in range(1, 6):
|
| 165 |
+
if _i == 3:
|
| 166 |
+
continue
|
| 167 |
+
print(_i)
|
| 168 |
+
_demonstrate_continue()
|
| 169 |
+
return
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
@app.cell(hide_code=True)
|
| 173 |
+
def _(mo):
|
| 174 |
+
mo.md("""
|
| 175 |
+
## Practical Loop Patterns
|
| 176 |
+
|
| 177 |
+
Here are some common patterns you'll use with loops:
|
| 178 |
+
|
| 179 |
+
```python
|
| 180 |
+
# Pattern 1: Accumulator
|
| 181 |
+
sum = 0
|
| 182 |
+
for num in [1, 2, 3, 4, 5]:
|
| 183 |
+
sum += num
|
| 184 |
+
|
| 185 |
+
# Pattern 2: Search
|
| 186 |
+
found = False
|
| 187 |
+
for item in items:
|
| 188 |
+
if condition:
|
| 189 |
+
found = True
|
| 190 |
+
break
|
| 191 |
+
|
| 192 |
+
# Pattern 3: Filter
|
| 193 |
+
filtered = []
|
| 194 |
+
for item in items:
|
| 195 |
+
if condition:
|
| 196 |
+
filtered.append(item)
|
| 197 |
+
```
|
| 198 |
+
""")
|
| 199 |
+
return
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
@app.cell(hide_code=True)
|
| 203 |
+
def _(mo):
|
| 204 |
+
callout_text = mo.md("""
|
| 205 |
+
## Loop Like a Pro!
|
| 206 |
+
|
| 207 |
+
Next Steps:
|
| 208 |
+
|
| 209 |
+
- Practice using different types of loops
|
| 210 |
+
- Experiment with loop control statements
|
| 211 |
+
- Try combining loops with lists and conditions
|
| 212 |
+
|
| 213 |
+
Keep iterating! 🔄✨
|
| 214 |
+
""")
|
| 215 |
+
|
| 216 |
+
mo.callout(callout_text, kind="success")
|
| 217 |
+
return (callout_text,)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
if __name__ == "__main__":
|
| 221 |
+
app.run()
|