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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 |
+
# 📦 Collections in Python
|
25 |
+
|
26 |
+
Explore Python's built-in collection types — the essential data structures!
|
27 |
+
|
28 |
+
## Lists
|
29 |
+
Lists are ordered, mutable sequences. Create them using square brackets:
|
30 |
+
|
31 |
+
```python
|
32 |
+
fruits = ["apple", "banana", "orange"]
|
33 |
+
numbers = [1, 2, 3, 4, 5]
|
34 |
+
mixed = [1, "hello", 3.14, True] # Can contain different types
|
35 |
+
```
|
36 |
+
|
37 |
+
Below is an example list we'll use to explore operations.
|
38 |
+
"""
|
39 |
+
)
|
40 |
+
return
|
41 |
+
|
42 |
+
|
43 |
+
@app.cell
|
44 |
+
def _():
|
45 |
+
sample_list = [1, 2, 3, 4, 5]
|
46 |
+
return (sample_list,)
|
47 |
+
|
48 |
+
|
49 |
+
@app.cell(hide_code=True)
|
50 |
+
def _(mo):
|
51 |
+
mo.md(
|
52 |
+
"""
|
53 |
+
## List Operations
|
54 |
+
|
55 |
+
Here are common operations you can perform on lists.
|
56 |
+
|
57 |
+
Try changing the values in `sample_list` above and watch the results change.
|
58 |
+
"""
|
59 |
+
)
|
60 |
+
return
|
61 |
+
|
62 |
+
|
63 |
+
@app.cell
|
64 |
+
def _(sample_list):
|
65 |
+
len(sample_list) # List length
|
66 |
+
return
|
67 |
+
|
68 |
+
|
69 |
+
@app.cell
|
70 |
+
def _(sample_list):
|
71 |
+
sample_list.append(6) # Add item to end
|
72 |
+
sample_list
|
73 |
+
return
|
74 |
+
|
75 |
+
|
76 |
+
@app.cell
|
77 |
+
def _(sample_list):
|
78 |
+
sample_list[0] # Access first element
|
79 |
+
return
|
80 |
+
|
81 |
+
|
82 |
+
@app.cell
|
83 |
+
def _(sample_list):
|
84 |
+
sample_list[-1] # Access last element
|
85 |
+
return
|
86 |
+
|
87 |
+
|
88 |
+
@app.cell(hide_code=True)
|
89 |
+
def _(mo):
|
90 |
+
mo.md(
|
91 |
+
"""
|
92 |
+
### Try marimo's Array Interface!
|
93 |
+
|
94 |
+
Explore how marimo handles arrays with this interactive element:
|
95 |
+
"""
|
96 |
+
)
|
97 |
+
return
|
98 |
+
|
99 |
+
|
100 |
+
@app.cell
|
101 |
+
def _(mo):
|
102 |
+
item = mo.ui.text(placeholder="Enter list item")
|
103 |
+
items = mo.ui.array([item] * 3, label="Create a list of 3 items")
|
104 |
+
return item, items
|
105 |
+
|
106 |
+
|
107 |
+
@app.cell
|
108 |
+
def _(items, mo):
|
109 |
+
mo.hstack(
|
110 |
+
[
|
111 |
+
items,
|
112 |
+
mo.md(f"Your list: {items.value}")
|
113 |
+
],
|
114 |
+
justify="space-between"
|
115 |
+
)
|
116 |
+
return
|
117 |
+
|
118 |
+
|
119 |
+
@app.cell(hide_code=True)
|
120 |
+
def _(mo):
|
121 |
+
_text = mo.md("""
|
122 |
+
- Try entering different types of items
|
123 |
+
- Watch how the list updates in real-time
|
124 |
+
|
125 |
+
This is a great way to experiment with list creation!
|
126 |
+
""")
|
127 |
+
mo.accordion({"💡 Interactive List Builder Tips": _text})
|
128 |
+
return
|
129 |
+
|
130 |
+
|
131 |
+
@app.cell(hide_code=True)
|
132 |
+
def _(mo):
|
133 |
+
mo.md(
|
134 |
+
"""
|
135 |
+
## Tuples
|
136 |
+
|
137 |
+
Tuples are immutable sequences. They're like lists that can't be changed after creation:
|
138 |
+
"""
|
139 |
+
)
|
140 |
+
return
|
141 |
+
|
142 |
+
|
143 |
+
@app.cell
|
144 |
+
def _():
|
145 |
+
coordinates = (10, 20)
|
146 |
+
return (coordinates,)
|
147 |
+
|
148 |
+
|
149 |
+
@app.cell
|
150 |
+
def _(coordinates):
|
151 |
+
x, y = coordinates # Tuple unpacking
|
152 |
+
x
|
153 |
+
return x, y
|
154 |
+
|
155 |
+
|
156 |
+
@app.cell(hide_code=True)
|
157 |
+
def _(mo):
|
158 |
+
mo.md(
|
159 |
+
"""
|
160 |
+
## Dictionaries
|
161 |
+
|
162 |
+
Dictionaries store key-value pairs. They're perfect for mapping relationships:
|
163 |
+
"""
|
164 |
+
)
|
165 |
+
return
|
166 |
+
|
167 |
+
|
168 |
+
@app.cell
|
169 |
+
def _():
|
170 |
+
person = {
|
171 |
+
"name": "John Doe",
|
172 |
+
"age": 25,
|
173 |
+
"city": "New York"
|
174 |
+
}
|
175 |
+
return (person,)
|
176 |
+
|
177 |
+
|
178 |
+
@app.cell
|
179 |
+
def _(person):
|
180 |
+
person["name"] # Access value by key
|
181 |
+
return
|
182 |
+
|
183 |
+
|
184 |
+
@app.cell
|
185 |
+
def _(person):
|
186 |
+
person.keys() # Get all keys
|
187 |
+
return
|
188 |
+
|
189 |
+
|
190 |
+
@app.cell
|
191 |
+
def _(person):
|
192 |
+
person.values() # Get all values
|
193 |
+
return
|
194 |
+
|
195 |
+
|
196 |
+
@app.cell(hide_code=True)
|
197 |
+
def _(mo):
|
198 |
+
mo.md(
|
199 |
+
"""
|
200 |
+
### Try marimo's Dictionary Interface!
|
201 |
+
|
202 |
+
Create your own dictionary using marimo's interactive elements:
|
203 |
+
"""
|
204 |
+
)
|
205 |
+
return
|
206 |
+
|
207 |
+
|
208 |
+
@app.cell
|
209 |
+
def _(mo):
|
210 |
+
key1 = mo.ui.text(placeholder="Key 1")
|
211 |
+
value1 = mo.ui.text(placeholder="Value 1")
|
212 |
+
key2 = mo.ui.text(placeholder="Key 2")
|
213 |
+
value2 = mo.ui.text(placeholder="Value 2")
|
214 |
+
|
215 |
+
dictionary = mo.ui.dictionary(
|
216 |
+
{
|
217 |
+
"First key": key1,
|
218 |
+
"First value": value1,
|
219 |
+
"Second key": key2,
|
220 |
+
"Second value": value2,
|
221 |
+
}
|
222 |
+
)
|
223 |
+
|
224 |
+
return dictionary, key1, key2, value1, value2
|
225 |
+
|
226 |
+
|
227 |
+
@app.cell
|
228 |
+
def _(dictionary, mo):
|
229 |
+
mo.hstack(
|
230 |
+
[
|
231 |
+
dictionary,
|
232 |
+
mo.md(f"Your dictionary: {dictionary.value}")
|
233 |
+
],
|
234 |
+
justify="space-between"
|
235 |
+
)
|
236 |
+
return
|
237 |
+
|
238 |
+
|
239 |
+
@app.cell(hide_code=True)
|
240 |
+
def _(mo):
|
241 |
+
_text = mo.md("""Enter key-value pairs to build your dictionary
|
242 |
+
|
243 |
+
- Watch how the dictionary updates as you type
|
244 |
+
|
245 |
+
- Try different types of values
|
246 |
+
|
247 |
+
This interactive builder helps understand dictionary structure!
|
248 |
+
""")
|
249 |
+
mo.accordion({"💡 Dictionary Builder Tips": _text})
|
250 |
+
return
|
251 |
+
|
252 |
+
|
253 |
+
@app.cell(hide_code=True)
|
254 |
+
def _(mo):
|
255 |
+
mo.md(
|
256 |
+
"""
|
257 |
+
## Sets
|
258 |
+
|
259 |
+
Sets are unordered collections of unique elements:
|
260 |
+
"""
|
261 |
+
)
|
262 |
+
return
|
263 |
+
|
264 |
+
|
265 |
+
@app.cell
|
266 |
+
def _():
|
267 |
+
numbers_set = {1, 2, 3, 3, 2, 1} # Duplicates are removed
|
268 |
+
return (numbers_set,)
|
269 |
+
|
270 |
+
|
271 |
+
@app.cell
|
272 |
+
def _(numbers_set):
|
273 |
+
numbers_set.add(4) # Add new element
|
274 |
+
numbers_set
|
275 |
+
return
|
276 |
+
|
277 |
+
|
278 |
+
@app.cell
|
279 |
+
def _():
|
280 |
+
set1 = {1, 2, 3}
|
281 |
+
set2 = {3, 4, 5}
|
282 |
+
set1.intersection(set2) # Find common elements
|
283 |
+
return set1, set2
|
284 |
+
|
285 |
+
|
286 |
+
@app.cell(hide_code=True)
|
287 |
+
def _(mo):
|
288 |
+
mo.md(
|
289 |
+
"""
|
290 |
+
## Collection Methods and Operations
|
291 |
+
|
292 |
+
Here are some common operations across collections:
|
293 |
+
|
294 |
+
```python
|
295 |
+
# Lists
|
296 |
+
my_list = [1, 2, 3]
|
297 |
+
my_list.insert(0, 0) # Insert at position
|
298 |
+
my_list.remove(2) # Remove first occurrence
|
299 |
+
my_list.sort() # Sort in place
|
300 |
+
sorted_list = sorted(my_list) # Return new sorted list
|
301 |
+
|
302 |
+
# Dictionaries
|
303 |
+
my_dict = {"a": 1}
|
304 |
+
my_dict.update({"b": 2}) # Add new key-value pairs
|
305 |
+
my_dict.get("c", "Not found") # Safe access with default
|
306 |
+
|
307 |
+
# Sets
|
308 |
+
set_a = {1, 2, 3}
|
309 |
+
set_b = {3, 4, 5}
|
310 |
+
set_a.union(set_b) # Combine sets
|
311 |
+
set_a.difference(set_b) # Elements in A but not in B
|
312 |
+
```
|
313 |
+
"""
|
314 |
+
)
|
315 |
+
return
|
316 |
+
|
317 |
+
|
318 |
+
@app.cell(hide_code=True)
|
319 |
+
def _(mo):
|
320 |
+
callout_text = mo.md("""
|
321 |
+
## Collection Mastery Awaits!
|
322 |
+
|
323 |
+
Next Steps:
|
324 |
+
|
325 |
+
- Practice list and dictionary comprehensions
|
326 |
+
- Experiment with nested collections
|
327 |
+
- Try combining different collection types
|
328 |
+
|
329 |
+
Keep organizing data! 🗃️✨
|
330 |
+
""")
|
331 |
+
|
332 |
+
mo.callout(callout_text, kind="success")
|
333 |
+
return (callout_text,)
|
334 |
+
|
335 |
+
|
336 |
+
if __name__ == "__main__":
|
337 |
+
app.run()
|