Spaces:
Sleeping
Sleeping
File size: 4,219 Bytes
5a29a34 |
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 |
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.14"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
# 🔄 Loops in Python
Let's explore how Python helps us repeat tasks efficiently with loops!
## Types of Loops
Python has two main types of loops:
```python
# For loop - when you know how many times to repeat
for i in range(5):
print(i)
# While loop - when you don't know how many repetitions
while condition:
do_something()
```
Let's start with a simple list to explore loops.
"""
)
return
@app.cell
def _():
sample_fruits = ["apple", "banana", "orange", "grape"]
return (sample_fruits,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## For Loop Basics
The for loop is perfect for iterating over sequences.
Try changing the `sample_fruits` list above and see how the output changes.
"""
)
return
@app.cell
def _(sample_fruits):
def _print_fruits():
for _fruit in sample_fruits:
print(f"I like {_fruit}s!")
_print_fruits()
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
## Using enumerate()
When you need both the item and its position, use enumerate():
""")
return
@app.cell
def _(sample_fruits):
def _print_enumerated():
for _idx, _fruit in enumerate(sample_fruits):
print(f"{_idx + 1}. {_fruit}")
_print_enumerated()
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
## Range in Loops
range() is a powerful function for generating sequences of numbers:
""")
return
@app.cell
def _():
def _demonstrate_range():
print("range(5):", list(range(5)))
print("range(2, 5):", list(range(2, 5)))
print("range(0, 10, 2):", list(range(0, 10, 2)))
_demonstrate_range()
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""## While Loop Basics
While loops continue as long as a condition is `True`.""")
return
@app.cell
def _():
def _count_up():
_count = 0
while _count < 5:
print(f"Count is {_count}")
_count += 1
_count_up()
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
## Loop Control Statements
Python provides several ways to control loop execution:
- `break`: Exit the loop immediately
- `continue`: Skip to the next iteration
- `else`: Execute when loop completes normally
""")
return
@app.cell
def _():
def _demonstrate_break():
for _i in range(1, 6):
if _i == 4:
break
print(_i)
print("Loop ended early!")
_demonstrate_break()
return
@app.cell
def _():
def _demonstrate_continue():
for _i in range(1, 6):
if _i == 3:
continue
print(_i)
_demonstrate_continue()
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""
## Practical Loop Patterns
Here are some common patterns you'll use with loops:
```python
# Pattern 1: Accumulator
sum = 0
for num in [1, 2, 3, 4, 5]:
sum += num
# Pattern 2: Search
found = False
for item in items:
if condition:
found = True
break
# Pattern 3: Filter
filtered = []
for item in items:
if condition:
filtered.append(item)
```
""")
return
@app.cell(hide_code=True)
def _(mo):
callout_text = mo.md("""
## Loop Like a Pro!
Next Steps:
- Practice using different types of loops
- Experiment with loop control statements
- Try combining loops with lists and conditions
Keep iterating! 🔄✨
""")
mo.callout(callout_text, kind="success")
return (callout_text,)
if __name__ == "__main__":
app.run()
|