Skip to content

Commit fb4308c

Browse files
committed
Mostly cleaning up instructions
1 parent ac40f20 commit fb4308c

56 files changed

Lines changed: 65004 additions & 2130 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Week_0_python/a_tutorial_strings_format.ipynb‎

Lines changed: 123 additions & 329 deletions
Large diffs are not rendered by default.

‎Week_0_python/b_tutorial_strings_edit_search.ipynb‎

Lines changed: 51 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"metadata": {},
2424
"source": [
2525
"## Example: Editing a string\n",
26-
"SOUNDS LIKE I'M YELLING! to Sounds like I'm NOT yelling\n",
26+
"Goal: convert \"SOUNDS LIKE I'M YELLING!\" to \"Sounds like I'm NOT yelling\"\n",
2727
"\n",
2828
"Problem break down:\n",
2929
"- First do the conversion to lower case\n",
@@ -32,12 +32,15 @@
3232
"- Next, stick in a \"not\"\n",
3333
"- Next, take the ! out\n",
3434
"\n",
35-
"Obviously, if I knew what the sentence was it would be a lot easier to just, well, make the new sentence... But imagine, instead, you're writing a general purpose routine to convert all-caps strings to lower-case ones..."
35+
"Obviously, if I knew what the sentence was it would be a lot easier to just, well, make the new sentence... But imagine, instead, you're writing a general purpose routine to convert all-caps strings to lower-case ones and fix grammar errors...\n",
36+
"\n",
37+
"TODO\n",
38+
"- Try taking out all of the spaces"
3639
]
3740
},
3841
{
3942
"cell_type": "code",
40-
"execution_count": 1,
43+
"execution_count": null,
4144
"metadata": {},
4245
"outputs": [],
4346
"source": [
@@ -47,17 +50,9 @@
4750
},
4851
{
4952
"cell_type": "code",
50-
"execution_count": 2,
53+
"execution_count": null,
5154
"metadata": {},
52-
"outputs": [
53-
{
54-
"name": "stdout",
55-
"output_type": "stream",
56-
"text": [
57-
"This is yelling: SOUNDS LIKE I'M YELLING!, this is not: sounds like i'm yelling!\n"
58-
]
59-
}
60-
],
55+
"outputs": [],
6156
"source": [
6257
"# convert to lower case - lower makes a copy of the string, converts the upper case letters, then returns the copy\n",
6358
"my_string_lower_case = my_string_upper_case.lower()\n",
@@ -68,36 +63,20 @@
6863
},
6964
{
7065
"cell_type": "code",
71-
"execution_count": 3,
66+
"execution_count": null,
7267
"metadata": {},
73-
"outputs": [
74-
{
75-
"name": "stdout",
76-
"output_type": "stream",
77-
"text": [
78-
"Sounds like i'm yelling!\n"
79-
]
80-
}
81-
],
68+
"outputs": [],
8269
"source": [
83-
"# Oops, should capitalize first letter:\n",
70+
"# Oops, should capitalize first letter - note capitalize, and not upper:\n",
8471
"my_string_cap = my_string_lower_case.capitalize()\n",
8572
"print(my_string_cap)"
8673
]
8774
},
8875
{
8976
"cell_type": "code",
90-
"execution_count": 4,
77+
"execution_count": null,
9178
"metadata": {},
92-
"outputs": [
93-
{
94-
"name": "stdout",
95-
"output_type": "stream",
96-
"text": [
97-
"Sounds like I'm yelling!\n"
98-
]
99-
}
100-
],
79+
"outputs": [],
10180
"source": [
10281
"# ... and replace the lower-case i with an upper case\n",
10382
"# Notice the use of \"\" so that we can use ' as a character\n",
@@ -107,17 +86,9 @@
10786
},
10887
{
10988
"cell_type": "code",
110-
"execution_count": 5,
89+
"execution_count": null,
11190
"metadata": {},
112-
"outputs": [
113-
{
114-
"name": "stdout",
115-
"output_type": "stream",
116-
"text": [
117-
"Sounds like I'm not yelling!\n"
118-
]
119-
}
120-
],
91+
"outputs": [],
12192
"source": [
12293
"# add in the \"not\" by finding the last space\n",
12394
"index_last_space = my_string_with_i_fixed.rfind(\" \")\n",
@@ -130,23 +101,22 @@
130101
},
131102
{
132103
"cell_type": "code",
133-
"execution_count": 6,
104+
"execution_count": null,
134105
"metadata": {},
135-
"outputs": [
136-
{
137-
"name": "stdout",
138-
"output_type": "stream",
139-
"text": [
140-
"Sounds like I'm not yelling\n"
141-
]
142-
}
143-
],
106+
"outputs": [],
144107
"source": [
145108
"# ... without the ! at the end - use :-1 to only copy up to the last element\n",
146109
"my_not_yelling_string_better = my_string_with_i_fixed[0:index_last_space] + \" not\" + my_string_with_i_fixed[index_last_space:-1]\n",
147110
"print(my_not_yelling_string_better)"
148111
]
149112
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"... Yes, you can do all of that editing in one line of really confusing Python. Just don't. BUT, you might try combining two steps at the same time..."
118+
]
119+
},
150120
{
151121
"cell_type": "markdown",
152122
"metadata": {},
@@ -160,21 +130,9 @@
160130
},
161131
{
162132
"cell_type": "code",
163-
"execution_count": 7,
133+
"execution_count": null,
164134
"metadata": {},
165-
"outputs": [
166-
{
167-
"ename": "TypeError",
168-
"evalue": "'str' object does not support item assignment",
169-
"output_type": "error",
170-
"traceback": [
171-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
172-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
173-
"\u001b[0;32m<ipython-input-7-a8cfd7393bb6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_not_yelling_string\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m's'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
174-
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
175-
]
176-
}
177-
],
135+
"outputs": [],
178136
"source": [
179137
"my_not_yelling_string[1] = 's'"
180138
]
@@ -189,7 +147,11 @@
189147
"\n",
190148
"For these examples we'll have pre-defined strings that are \"typed in\" (rather than reading them from a file) just to keep things simple. In reality, you usually have to do this when you can't edit the files directly.\n",
191149
"\n",
192-
"An aside: When you only have one file you're \"cleaning up\" for processing (as opposed to writing some code to process a whole bunch of similar files) it is often a lot easier to open the file in your favorite spreadsheet editor and just... edit it there. "
150+
"An aside: When you only have one file you're \"cleaning up\" for processing (as opposed to writing some code to process a whole bunch of similar files) it is often a lot easier to open the file in your favorite spreadsheet editor and just... edit it there. \n",
151+
"\n",
152+
"TODO:\n",
153+
"- Create a string with semi-colons separating the values\n",
154+
"- Use split to pull it apart into a list of numbers"
193155
]
194156
},
195157
{
@@ -202,17 +164,9 @@
202164
},
203165
{
204166
"cell_type": "code",
205-
"execution_count": 8,
167+
"execution_count": null,
206168
"metadata": {},
207-
"outputs": [
208-
{
209-
"name": "stdout",
210-
"output_type": "stream",
211-
"text": [
212-
"\t0.3, 0.4, 0.5, 3., 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2\n"
213-
]
214-
}
215-
],
169+
"outputs": [],
216170
"source": [
217171
"# Make a list of numbers\n",
218172
"list_of_numbers = [0.3 + 0.1 * n for n in range(0, 10)]\n",
@@ -226,17 +180,9 @@
226180
},
227181
{
228182
"cell_type": "code",
229-
"execution_count": 9,
183+
"execution_count": null,
230184
"metadata": {},
231-
"outputs": [
232-
{
233-
"name": "stdout",
234-
"output_type": "stream",
235-
"text": [
236-
"[0.3, 0.4, 0.5, 3.0, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]\n"
237-
]
238-
}
239-
],
185+
"outputs": [],
240186
"source": [
241187
"# Take it back apart again using split, looking for commans\n",
242188
"my_list_back_as_strs = str_list_of_numbers.split(',')\n",
@@ -248,7 +194,7 @@
248194
},
249195
{
250196
"cell_type": "code",
251-
"execution_count": 10,
197+
"execution_count": null,
252198
"metadata": {},
253199
"outputs": [],
254200
"source": [
@@ -267,44 +213,28 @@
267213
"We have a list of strings, which consist of a descriptive name followed by a colon followed by a number\n",
268214
" - Find the end of the line (the \\n) then split up each line.\n",
269215
" - If the line has a : then get out the number, otherwise, ignore it\n",
270-
" - Note that there is a more complete example of this (for building a dictionary) in the dictionary tutorial"
216+
" - Note that there is a more complete example of this (for building a dictionary) in the dictionary tutorial\n",
217+
" \n",
218+
"TODO\n",
219+
"- Try printing out which item it is when you find one\n",
220+
"- To test it, reverse the order in the string and see if it still works"
271221
]
272222
},
273223
{
274224
"cell_type": "code",
275-
"execution_count": 12,
225+
"execution_count": null,
276226
"metadata": {},
277-
"outputs": [
278-
{
279-
"name": "stdout",
280-
"output_type": "stream",
281-
"text": [
282-
"# This is a header\n",
283-
"Item 1: 0.3\n",
284-
"Blank line\n",
285-
"Item 2: 1.3\n",
286-
"\n"
287-
]
288-
}
289-
],
227+
"outputs": [],
290228
"source": [
291229
"my_str_to_parse = \"# This is a header\\nItem 1: 0.3\\nBlank line\\nItem 2: 1.3\\n\"\n",
292230
"print(my_str_to_parse)"
293231
]
294232
},
295233
{
296234
"cell_type": "code",
297-
"execution_count": 14,
235+
"execution_count": null,
298236
"metadata": {},
299-
"outputs": [
300-
{
301-
"name": "stdout",
302-
"output_type": "stream",
303-
"text": [
304-
"['# This is a header', 'Item 1: 0.3', 'Blank line', 'Item 2: 1.3', '']\n"
305-
]
306-
}
307-
],
237+
"outputs": [],
308238
"source": [
309239
"# First, split the line into strings separated by \\n\n",
310240
"my_lines = my_str_to_parse.split('\\n')\n",
@@ -313,17 +243,9 @@
313243
},
314244
{
315245
"cell_type": "code",
316-
"execution_count": 15,
246+
"execution_count": null,
317247
"metadata": {},
318-
"outputs": [
319-
{
320-
"name": "stdout",
321-
"output_type": "stream",
322-
"text": [
323-
"[0.3, 1.3]\n"
324-
]
325-
}
326-
],
248+
"outputs": [],
327249
"source": [
328250
"# An empty list of numbers\n",
329251
"my_numbers = []\n",
@@ -334,6 +256,7 @@
334256
" # split again - you could also use find and the resulting index\n",
335257
" two_parts = l.split(\":\")\n",
336258
" # Don't forget to convert the string to the number\n",
259+
" #. the [-1] is to get the last (second) element in the list\n",
337260
" the_number = float(two_parts[-1])\n",
338261
" my_numbers.append(the_number)\n",
339262
"print(my_numbers)"
@@ -344,22 +267,14 @@
344267
"metadata": {},
345268
"source": [
346269
"## Tutorial: Editing a file name\n",
347-
"This is pretty common - take the last 4 characters off and replace them with a different file type"
270+
"This is pretty common - take the last 4 characters off and replace them with a different file type. Try changing .txt to .py and edit the second line to do the right thing."
348271
]
349272
},
350273
{
351274
"cell_type": "code",
352-
"execution_count": 17,
275+
"execution_count": null,
353276
"metadata": {},
354-
"outputs": [
355-
{
356-
"name": "stdout",
357-
"output_type": "stream",
358-
"text": [
359-
"my_file.csv\n"
360-
]
361-
}
362-
],
277+
"outputs": [],
363278
"source": [
364279
"file_name_orig = \"my_file.txt\"\n",
365280
"file_name_new = file_name_orig[0:-4] + \".csv\"\n",

0 commit comments

Comments
 (0)