You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: slides_sources/source/exercises/string_formatting.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ A Couple Exercises
33
33
34
34
and produce:
35
35
36
-
``'file_002 : 123.46, 1e+04'``
36
+
``'file_002 : 123.46, 1.00e+04'``
37
37
38
38
**Note:** the idea behind the "file_002" is that if you have a bunch of files that you want to name with numbers that can be sorted, you need to "pad" the numbers with zeros to get the right sort order.
Copy file name to clipboardExpand all lines: slides_sources/source/session04.rst
+65-16Lines changed: 65 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,6 @@
4
4
Session Four: Dictionaries, Sets, and Files
5
5
*******************************************
6
6
7
-
8
7
================
9
8
Review/Questions
10
9
================
@@ -35,11 +34,13 @@ Lightning Talks Today:
35
34
36
35
.. rst-class:: mlarge
37
36
38
-
Andrey Gusev
37
+
Abdishu Hagi
38
+
39
+
Enrique R Silva
39
40
40
-
Cheryl Ohashi
41
+
Isaac Cowhey
41
42
42
-
Maxwell MacCamy
43
+
Paul G Anderson
43
44
44
45
45
46
==============================
@@ -68,7 +69,7 @@ You can do that in a for loop, also:
68
69
In [4]: l = [(1, 2), (3, 4), (5, 6)]
69
70
70
71
In [5]: for i, j in l:
71
-
print("i:%i, j:%i"%(i, j))
72
+
print("i:{}, j:{}".format(i, j))
72
73
73
74
i:1, j:2
74
75
i:3, j:4
@@ -77,8 +78,8 @@ You can do that in a for loop, also:
77
78
(Mailroom example)
78
79
79
80
80
-
Looping through two loops at once:
81
-
----------------------------------
81
+
Looping through two iterables at once:
82
+
--------------------------------------
82
83
83
84
.. rst-class:: mlarge
84
85
@@ -91,8 +92,8 @@ Looping through two loops at once:
91
92
In [11]: l2 = [3, 4, 5]
92
93
93
94
In [12]: for i, j in zip(l1, l2):
94
-
....: print("i:%i, j:%i"%(i, j))
95
-
....:
95
+
print("i:{}, j:{}".format(i, j))
96
+
96
97
i:1, j:3
97
98
i:2, j:4
98
99
i:3, j:5
@@ -116,7 +117,7 @@ Need the index and the item?
116
117
In [2]: l = ['this', 'that', 'the other']
117
118
118
119
In [3]: for i, item in enumerate(l):
119
-
...: print("the %ith item is: %s"%(i, item))
120
+
...: print("the {:d}th item is: {:s}".format(i, item))
120
121
...:
121
122
the 0th item is: this
122
123
the 1th item is: that
@@ -171,10 +172,25 @@ What is this::
171
172
172
173
about?
173
174
175
+
Every module has a __name__
176
+
177
+
If the module is loaded by ``import`` then it's name is the filename.
178
+
179
+
If the module is run at the command line, like:
180
+
181
+
.. code-block:: bash
182
+
183
+
python3 the_module.py
184
+
185
+
Then it's ``__name__`` will be "__main__"
186
+
187
+
This can be used to run code only when a module is run as a command,
188
+
but not when it is imported.
189
+
174
190
(demo)
175
191
176
192
assert
177
-
--------
193
+
------
178
194
179
195
What is ``assert`` for?
180
196
@@ -187,10 +203,33 @@ in operational code should be::
187
203
if m < 0:
188
204
raise ValueError
189
205
190
-
I'll cover next week ...
206
+
I'll cover more next week ...
191
207
192
208
(Asserts get ignored if optimization is turned on!)
193
209
210
+
what the heck is reversed()?
211
+
----------------------------
212
+
213
+
I had a question in a PR:
214
+
215
+
"what is ``reversed(x)``'s resultant object? what good is it?""
216
+
217
+
.. nextslide::
218
+
219
+
try it:
220
+
221
+
.. code-block:: ipython
222
+
223
+
In [14]: type(reversed(l))
224
+
Out[14]: list_reverseiterator
225
+
226
+
so it's a ``list_reverseiterator`` object -- not helpful, is it :-)
227
+
228
+
But what it means is that it's an "iterable" that you can then do things like loop through with a for loop, etc. but it hasn't made a copy of the list -- it returns the items one by one as they are asked for. this has performance benefits, as it doesn't have to make a copy of the whole thing.
229
+
230
+
So you use it if you want to loop through something in reversed order, but dont actually need an actual list with the order reversed.
231
+
232
+
we'll get more into the details of iterators and iterables later in the class.
194
233
195
234
=================
196
235
A little warm up
@@ -203,6 +242,15 @@ Fun with strings
203
242
204
243
- for an arbitrary number of numbers...
205
244
245
+
===============
246
+
Lightning Talks
247
+
===============
248
+
249
+
|
250
+
|Isaac Cowhey
251
+
|
252
+
|Paul G Anderson
253
+
|
206
254
207
255
=====================
208
256
Dictionaries and Sets
@@ -662,13 +710,14 @@ Have some fun with dictionaries and sets!
0 commit comments