Akshay Agrawal commited on
Commit
0127ce9
·
unverified ·
2 Parent(s): be305c4 3378349

Merge pull request #2 from marimo-team/haleshot/collections

Browse files
Files changed (1) hide show
  1. Python/phase_1/collections.py +254 -0
Python/phase_1/collections.py ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ extended_list = sample_list + [6] # concatenate two lists
72
+ extended_list
73
+ return (extended_list,)
74
+
75
+
76
+ @app.cell
77
+ def _(extended_list):
78
+ extended_list[0] # Access first element
79
+ return
80
+
81
+
82
+ @app.cell
83
+ def _(extended_list):
84
+ extended_list[-1] # Access last element
85
+ return
86
+
87
+
88
+ @app.cell(hide_code=True)
89
+ def _(mo):
90
+ mo.md(
91
+ """
92
+ ## Tuples
93
+
94
+ Tuples are immutable sequences. They're like lists that can't be changed after creation:
95
+ """
96
+ )
97
+ return
98
+
99
+
100
+ @app.cell
101
+ def _():
102
+ coordinates = (10, 20)
103
+ return (coordinates,)
104
+
105
+
106
+ @app.cell
107
+ def _(coordinates):
108
+ x, y = coordinates # Tuple unpacking
109
+ x
110
+ return x, y
111
+
112
+
113
+ @app.cell(hide_code=True)
114
+ def _(mo):
115
+ mo.md("""#### Tuple concatenation""")
116
+ return
117
+
118
+
119
+ @app.cell
120
+ def _():
121
+ tuple1 = (1, 2, 3)
122
+ tuple2 = (4, 5, 6)
123
+
124
+ tuple3 = tuple1 + tuple2
125
+
126
+ tuple3
127
+ return tuple1, tuple2, tuple3
128
+
129
+
130
+ @app.cell(hide_code=True)
131
+ def _(mo):
132
+ mo.md(
133
+ """
134
+ ## Dictionaries
135
+
136
+ Dictionaries store key-value pairs. They're perfect for mapping relationships:
137
+ """
138
+ )
139
+ return
140
+
141
+
142
+ @app.cell
143
+ def _():
144
+ person = {
145
+ "name": "John Doe",
146
+ "age": 25,
147
+ "city": "New York"
148
+ }
149
+ return (person,)
150
+
151
+
152
+ @app.cell
153
+ def _(person):
154
+ person["name"] # Access value by key
155
+ return
156
+
157
+
158
+ @app.cell
159
+ def _(person):
160
+ person.keys() # Get all keys
161
+ return
162
+
163
+
164
+ @app.cell
165
+ def _(person):
166
+ person.values() # Get all values
167
+ return
168
+
169
+
170
+ @app.cell(hide_code=True)
171
+ def _(mo):
172
+ mo.md(
173
+ """
174
+ ## Sets
175
+
176
+ Sets are unordered collections of unique elements:
177
+ """
178
+ )
179
+ return
180
+
181
+
182
+ @app.cell
183
+ def _():
184
+ numbers_set = {1, 2, 3, 3, 2, 1} # Duplicates are removed
185
+ return (numbers_set,)
186
+
187
+
188
+ @app.cell
189
+ def _(numbers_set):
190
+ numbers_set.add(4) # Add new element
191
+ numbers_set
192
+ return
193
+
194
+
195
+ @app.cell
196
+ def _():
197
+ set1 = {1, 2, 3}
198
+ set2 = {3, 4, 5}
199
+ set1.intersection(set2) # Find common elements
200
+ return set1, set2
201
+
202
+
203
+ @app.cell(hide_code=True)
204
+ def _(mo):
205
+ mo.md(
206
+ """
207
+ ## Collection Methods and Operations
208
+
209
+ Here are some common operations across collections:
210
+
211
+ ```python
212
+ # Lists
213
+ my_list = [1, 2, 3]
214
+ my_list.insert(0, 0) # Insert at position
215
+ my_list.remove(2) # Remove first occurrence
216
+ my_list.sort() # Sort in place
217
+ sorted_list = sorted(my_list) # Return new sorted list
218
+
219
+ # Dictionaries
220
+ my_dict = {"a": 1}
221
+ my_dict.update({"b": 2}) # Add new key-value pairs
222
+ my_dict.get("c", "Not found") # Safe access with default
223
+
224
+ # Sets
225
+ set_a = {1, 2, 3}
226
+ set_b = {3, 4, 5}
227
+ set_a.union(set_b) # Combine sets
228
+ set_a.difference(set_b) # Elements in A but not in B
229
+ ```
230
+ """
231
+ )
232
+ return
233
+
234
+
235
+ @app.cell(hide_code=True)
236
+ def _(mo):
237
+ callout_text = mo.md("""
238
+ ## Collection Mastery Awaits!
239
+
240
+ Next Steps:
241
+
242
+ - Practice list and dictionary comprehensions
243
+ - Experiment with nested collections
244
+ - Try combining different collection types
245
+
246
+ Keep organizing data! 🗃️✨
247
+ """)
248
+
249
+ mo.callout(callout_text, kind="success")
250
+ return (callout_text,)
251
+
252
+
253
+ if __name__ == "__main__":
254
+ app.run()