Skip to content

Commit 1b5bbbd

Browse files
committed
update for class
1 parent 5ff410f commit 1b5bbbd

File tree

2 files changed

+134
-29
lines changed

2 files changed

+134
-29
lines changed

lectures/01-python/python-datatypes.ipynb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 2,
66
"metadata": {
77
"collapsed": true
88
},
@@ -65,24 +65,24 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": 2,
68+
"execution_count": 4,
6969
"metadata": {
7070
"collapsed": false
7171
},
7272
"outputs": [
7373
{
7474
"data": {
7575
"text/plain": [
76-
"4"
76+
"7"
7777
]
7878
},
79-
"execution_count": 2,
79+
"execution_count": 4,
8080
"metadata": {},
8181
"output_type": "execute_result"
8282
}
8383
],
8484
"source": [
85-
"2+2"
85+
"2+2+3"
8686
]
8787
},
8888
{
@@ -279,7 +279,7 @@
279279
},
280280
{
281281
"cell_type": "code",
282-
"execution_count": 11,
282+
"execution_count": 5,
283283
"metadata": {
284284
"collapsed": false
285285
},
@@ -290,7 +290,7 @@
290290
},
291291
{
292292
"cell_type": "code",
293-
"execution_count": 12,
293+
"execution_count": 6,
294294
"metadata": {
295295
"collapsed": false
296296
},
@@ -309,7 +309,7 @@
309309
},
310310
{
311311
"cell_type": "code",
312-
"execution_count": 13,
312+
"execution_count": 7,
313313
"metadata": {
314314
"collapsed": false
315315
},
@@ -320,7 +320,7 @@
320320
},
321321
{
322322
"cell_type": "code",
323-
"execution_count": 14,
323+
"execution_count": 8,
324324
"metadata": {
325325
"collapsed": false
326326
},
@@ -346,7 +346,7 @@
346346
},
347347
{
348348
"cell_type": "code",
349-
"execution_count": 15,
349+
"execution_count": 9,
350350
"metadata": {
351351
"collapsed": false
352352
},
@@ -602,7 +602,7 @@
602602
},
603603
{
604604
"cell_type": "code",
605-
"execution_count": 16,
605+
"execution_count": 10,
606606
"metadata": {
607607
"collapsed": false
608608
},

lectures/01-python/python-functions.ipynb

Lines changed: 123 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818
"# functions"
1919
]
2020
},
21+
{
22+
"cell_type": "raw",
23+
"metadata": {},
24+
"source": [
25+
"Functions are used to organize program flow, especially to allow us to easily do commonly needed tasks over and over again. We've already used a lot of functions, such as those that work on lists (`append()` and `pop()`) or strings (like `replace()`). Here we see how to write our own functions"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"A function takes arguments, listed in the `()` and returns a value. Even if you don't explictly give a return value, one will be return (e.g., `None`). \n",
33+
"\n",
34+
"Here's a simple example of a function that takes a single argument, `i`"
35+
]
36+
},
2137
{
2238
"cell_type": "code",
2339
"execution_count": 2,
@@ -46,7 +62,9 @@
4662
"cell_type": "markdown",
4763
"metadata": {},
4864
"source": [
49-
"functions are one place where scope comes into play. A function has its own namespace. If a variable is not defined in that function, then it will look to the namespace from where it was called to see if that variable exists there. "
65+
"functions are one place where _scope_ comes into play. A function has its own _namespace_. If a variable is not defined in that function, then it will look to the namespace from where it was called to see if that variable exists there. \n",
66+
"\n",
67+
"However, you should avoid this as much as possible (variables that persist across namespaces are called global variables)."
5068
]
5169
},
5270
{
@@ -112,7 +130,7 @@
112130
"cell_type": "markdown",
113131
"metadata": {},
114132
"source": [
115-
"functions always return a value -- if one is not explicitly given, then they return None, otherwise, they can return values (even multiple values) of any type"
133+
"By default, python will let you read from a global, but not update it."
116134
]
117135
},
118136
{
@@ -121,6 +139,42 @@
121139
"metadata": {
122140
"collapsed": false
123141
},
142+
"outputs": [
143+
{
144+
"name": "stdout",
145+
"output_type": "stream",
146+
"text": [
147+
"in function outer = -100.0\n",
148+
"outside, outer = 1.0\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"outer = 1.0\n",
154+
"\n",
155+
"def update():\n",
156+
" # uncomment this to allow us to access outer in the calling namespace\n",
157+
" # global outer\n",
158+
" outer = -100.0\n",
159+
" print(\"in function outer = {}\".format(outer))\n",
160+
" \n",
161+
"update()\n",
162+
"print(\"outside, outer = {}\".format(outer))"
163+
]
164+
},
165+
{
166+
"cell_type": "markdown",
167+
"metadata": {},
168+
"source": [
169+
"functions always return a value -- if one is not explicitly given, then they return None, otherwise, they can return values (even multiple values) of any type"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 7,
175+
"metadata": {
176+
"collapsed": false
177+
},
124178
"outputs": [
125179
{
126180
"name": "stdout",
@@ -136,9 +190,49 @@
136190
"print(a)"
137191
]
138192
},
193+
{
194+
"cell_type": "markdown",
195+
"metadata": {},
196+
"source": [
197+
"None is a special quantity in python (analogous to `null` in some other languages). We can test on `None` -- the preferred manner is to use `is`:"
198+
]
199+
},
139200
{
140201
"cell_type": "code",
141-
"execution_count": 7,
202+
"execution_count": 8,
203+
"metadata": {
204+
"collapsed": false
205+
},
206+
"outputs": [
207+
{
208+
"name": "stdout",
209+
"output_type": "stream",
210+
"text": [
211+
"we didn't do anything\n"
212+
]
213+
}
214+
],
215+
"source": [
216+
"def do_nothing():\n",
217+
" pass\n",
218+
"\n",
219+
"a = do_nothing()\n",
220+
"if a is None:\n",
221+
" print(\"we didn't do anything\")"
222+
]
223+
},
224+
{
225+
"cell_type": "markdown",
226+
"metadata": {},
227+
"source": [
228+
"## More Complex Functions\n",
229+
"\n",
230+
"Here's a more complex example. We return a pair of variables -- in python this is done by packing them into a tuple and then unpacking on the calling end. Also note the _docstring_ here."
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": 9,
142236
"metadata": {
143237
"collapsed": false
144238
},
@@ -162,9 +256,9 @@
162256
" a, b = b, a+b\n",
163257
" return result, len(result)\n",
164258
"\n",
165-
"list, n = fib2(250)\n",
259+
"fib, n = fib2(250)\n",
166260
"print(n)\n",
167-
"print(list)"
261+
"print(fib)"
168262
]
169263
},
170264
{
@@ -176,7 +270,7 @@
176270
},
177271
{
178272
"cell_type": "code",
179-
"execution_count": 8,
273+
"execution_count": 10,
180274
"metadata": {
181275
"collapsed": false
182276
},
@@ -206,7 +300,7 @@
206300
},
207301
{
208302
"cell_type": "code",
209-
"execution_count": 9,
303+
"execution_count": 11,
210304
"metadata": {
211305
"collapsed": false
212306
},
@@ -237,12 +331,14 @@
237331
"source": [
238332
"it is important to note that python evaluates the optional arguments once--when the function is defined. This means that if you make the default an empty object, for instance, it will persist across all calls.\n",
239333
"\n",
240-
"** This is one of the most common errors for beginners **"
334+
"** This is one of the most common errors for beginners **\n",
335+
"\n",
336+
"Here's an example of trying to initialize to an empty list:"
241337
]
242338
},
243339
{
244340
"cell_type": "code",
245-
"execution_count": 10,
341+
"execution_count": 12,
246342
"metadata": {
247343
"collapsed": false
248344
},
@@ -271,12 +367,14 @@
271367
"cell_type": "markdown",
272368
"metadata": {},
273369
"source": [
274-
"instead do"
370+
"Notice that each call does not create its own separate list. Instead a single empty list was created when the function was first processed, and this list persists in memory as the default value for the optional argument `L`. \n",
371+
"\n",
372+
"If we want a unique list created each time (e.g., a separate place in memory), we instead do"
275373
]
276374
},
277375
{
278376
"cell_type": "code",
279-
"execution_count": 11,
377+
"execution_count": 13,
280378
"metadata": {
281379
"collapsed": false
282380
},
@@ -303,6 +401,13 @@
303401
"print(fnew(3))"
304402
]
305403
},
404+
{
405+
"cell_type": "markdown",
406+
"metadata": {},
407+
"source": [
408+
"Notice that the same `None` that we saw previously comes into play here. "
409+
]
410+
},
306411
{
307412
"cell_type": "markdown",
308413
"metadata": {
@@ -318,12 +423,12 @@
318423
"source": [
319424
"Lambdas are \"disposible\" functions. These are small, nameless functions that are often used as arguments in other functions.\n",
320425
"\n",
321-
"Ex, from the official tutorial:"
426+
"Ex, from the official tutorial: we have a list of tuples. We want to sort the list based on the second item in the tuple. The `sort` method can take a `key` optional argument that tells us how to interpret the list item for sorting"
322427
]
323428
},
324429
{
325430
"cell_type": "code",
326-
"execution_count": 3,
431+
"execution_count": 14,
327432
"metadata": {
328433
"collapsed": true
329434
},
@@ -335,7 +440,7 @@
335440
},
336441
{
337442
"cell_type": "code",
338-
"execution_count": 4,
443+
"execution_count": 15,
339444
"metadata": {
340445
"collapsed": false
341446
},
@@ -346,7 +451,7 @@
346451
"[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]"
347452
]
348453
},
349-
"execution_count": 4,
454+
"execution_count": 15,
350455
"metadata": {},
351456
"output_type": "execute_result"
352457
}
@@ -364,7 +469,7 @@
364469
},
365470
{
366471
"cell_type": "code",
367-
"execution_count": 12,
472+
"execution_count": 16,
368473
"metadata": {
369474
"collapsed": false
370475
},
@@ -391,7 +496,7 @@
391496
" 9216]"
392497
]
393498
},
394-
"execution_count": 12,
499+
"execution_count": 16,
395500
"metadata": {},
396501
"output_type": "execute_result"
397502
}
@@ -437,7 +542,7 @@
437542
"name": "python",
438543
"nbconvert_exporter": "python",
439544
"pygments_lexer": "ipython3",
440-
"version": "3.4.3"
545+
"version": "3.5.2"
441546
}
442547
},
443548
"nbformat": 4,

0 commit comments

Comments
 (0)