-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path05-python-intro.html
643 lines (603 loc) · 30.5 KB
/
05-python-intro.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Introduction to Bioinformatics</title>
<meta name="description" content="Introduction to Bioinformatics">
<!-- Disabled for now - embeds HTML in author string -->
<!-- <meta name="author" content="Lesson 5 - Python basics" /> -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="src/theme/csmall.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="reveal.js/css/print/' +
( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) +
'.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<!-- Note escaped dollars -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Introduction to Bioinformatics</h1>
<h3>Lesson 5 - Python basics</h3>
<p>
<h4></h4>
</p>
</section>
<section id="what-well-cover-today" class="level1">
<h1>What we’ll cover today</h1>
<p>Big picture overview and contextualization of Python:</p>
<ul>
<li>Basic data types: What kinds of data are available</li>
<li>Syntax: What constitutes a valid python expression</li>
<li>Variables: How we keep track of and pass around python data</li>
<li>Control flow: How we specify the behaviour of our program</li>
<li>Functions: How we package functionality</li>
</ul>
<p>Conceptually, 90% of python can be explained in terms of these things</p>
</section>
<section id="next-week" class="level1">
<h1>Next week</h1>
<p>We’ll fill in the final 90%, and start building our own data structures using Object Oriented Programming (OOP).</p>
<p>These concepts can be explained largely in terms of those from this session.</p>
</section>
<section id="filling-in-the-pieces" class="level1">
<h1>Filling in the pieces</h1>
<p>Please refer to the Codecademy <a href="http://www.codecademy.com/tracks/python">Python course</a> for more practice and fluency with these concepts, and how they relate to each other.</p>
</section>
<section id="context-motivation" class="level1">
<h1>Context & motivation</h1>
<p>Why do we care about Python?</p>
</section>
<section id="whywhen-shell-scripts" class="level1">
<h1>Why/when shell scripts?</h1>
<ul>
<li>Plumbing together existing command line programs (duct tape)…</li>
</ul>
<p>Not so good for actual logic or anything too complex</p>
</section>
<section id="whywhen-r" class="level1">
<h1>Why/when R?</h1>
<ul>
<li>Very tabular data (<code>data.frame</code>s are great)</li>
<li>Very tabular data operations (plyr, dplyr, reshape2)</li>
<li>Plotting (ggplot2, shiny)</li>
<li>Ready to go statistics functions</li>
</ul>
<p>However, R has some pain points…</p>
</section>
<section id="whywhen-python" class="level1">
<h1>Why/when python?</h1>
<ul>
<li>More flexible data structures</li>
<li>Better performance</li>
<li>More flexible / cleaner programming models => better for algorithmic development</li>
<li>Better organization for building bigger programs</li>
<li>Better stream (Unix stdin/stdout) support than R</li>
</ul>
</section>
<section id="together-the-three-make-a-great-combination" class="level1">
<h1>Together, the three make a great combination</h1>
<p>Don’t limit yourself unnecessarily to one tool…</p>
<p>Living in Unix makes it easy to use both R and Python in the context of a single project.</p>
</section>
<section id="other-languages" class="level1">
<h1>Other languages?</h1>
<p>There are other languages that fit some of these characteristics, but python is nice because</p>
<ul>
<li>It’s become popular amongst the scientific community</li>
<li>It’s relatively simpler in design than other languages</li>
<li>It’s relatively quick to write (not necessarily to run)</li>
</ul>
</section>
<section id="python-basics" class="level1">
<h1>Python Basics</h1>
<p><em>In which we investigate the building blocks of logic</em></p>
</section>
<section id="firing-up-a-python-repl" class="level1">
<h1>Firing up a python “REPL”</h1>
<p>REPL is short for Read Evaluate Print Loop.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">python
<span class="op">></span> <span class="bu">print</span> <span class="st">"Hello world"</span></code></pre></div>
<p>Now you can enter in little bits of code as we go (type Ctrl-d or <code>exit</code> to get out)</p>
</section>
<section id="basic-data-types" class="level1">
<h1>Basic data types</h1>
<p><em>The elements of logic; our nouns</em></p>
</section>
<section id="questioning-the-unix-plain-text-philosophy" class="level1">
<h1>Questioning the Unix plain text philosophy</h1>
<p>The Unix philosophy embraces plain text data.</p>
<ul>
<li>In theory means universal interoperability, and composability</li>
<li>But at the expense of richness/standardization in the structure of data.</li>
</ul>
</section>
<section id="python-has-many-data-types" class="level1">
<h1>Python has many data types</h1>
<p>Data types let us more easily manage information, and relate it to the things in the world it represents.</p>
<p>This comes at the expense of universal interoperability.</p>
</section>
<section id="scalar-data-types" class="level1">
<h1>Scalar data types</h1>
<p>The most basic data types are <em>scalars</em>. We think of them as <em>atomic</em> values.</p>
<ul>
<li>strings: <code>"this is a string"</code></li>
<li>ints: <code>42</code></li>
<li>floats: <code>3.14159</code></li>
<li>booleans: <code>True</code></li>
</ul>
</section>
<section id="collection-types" class="level1">
<h1>Collection types</h1>
<p>These are “containers” for other types of data (scalars, other collections, more complex data types…).</p>
</section>
<section id="collection-types-1" class="level1">
<h1>Collection types</h1>
<ul>
<li>tuples: <code>("Bob Jones", 42)</code>
<ul>
<li>immutable & fixed length</li>
<li>efficient and <em>hashable</em></li>
</ul></li>
<li>dictionaries: <code>{"name": "Bob Jones", "age": 42}</code>
<ul>
<li>mutable & variable length</li>
<li>unordered “key/value pairs”</li>
<li>often for representing data about some <em>thing</em></li>
</ul></li>
<li>lists: <code>["Bob Jones", "Jane Doe", "Ralph Nader"]</code>
<ul>
<li>mutable & variable length</li>
<li>ordered & positionally indexed</li>
<li>for collecting <em>things</em></li>
</ul></li>
</ul>
</section>
<section id="when-we-use-these" class="level1">
<h1>When we use these</h1>
<p>Tuples and dictionaries for organizing data about some single thing:</p>
<ul>
<li><code>("Bob Jones", 42)</code></li>
<li><code>{"name": "Bob Jones", "age": 42}</code></li>
</ul>
<p>Lists for arbitrary collections of individual data points:</p>
<ul>
<li><code>["Bob Jones", "Jane Doe", "Ralph Nader"]</code></li>
<li><code>[("Bob Jones", 42), ("Jane Doe", 31), ("Ralph N.", 85)]</code></li>
</ul>
</section>
<section id="sometimes-dictionaries-blur-the-lines-here" class="level1">
<h1>Sometimes dictionaries blur the lines here;</h1>
<p>Here we use them more or less like a list, but where we index entries by <em>name</em> instead of position:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">{<span class="st">"Bob Jones"</span>: {<span class="st">"age"</span>: <span class="dv">42</span>, <span class="st">"occupation"</span>: <span class="st">"haxxor"</span>},
<span class="st">"Jane Doe"</span>: {<span class="st">"age"</span>: <span class="dv">31</span>, <span class="st">"occupation"</span>: <span class="st">"mathematician"</span>},
<span class="st">"Ralph Nader"</span>: {<span class="st">"age"</span>: <span class="dv">85</span>, <span class="st">"occupation"</span>: <span class="st">"politician"</span>}}</code></pre></div>
</section>
<section id="other-collection-types" class="level1">
<h1>Other collection types</h1>
<p>We won’t go into these as deeply today, but in short, we tend to use them like lists:</p>
<ul>
<li>generators:
<ul>
<li>“recipes” for a collection</li>
<li>generates each thing as you need it</li>
<li>good for “big data”</li>
</ul></li>
<li>sets
<ul>
<li>unique entries</li>
<li>no order guarantees</li>
<li>mutable & variable length</li>
</ul></li>
</ul>
</section>
<section id="operations" class="level1">
<h1>Operations</h1>
<p><em>In which we manipulate and reason about objects; our verbs</em></p>
</section>
<section id="numeric" class="level1">
<h1>Numeric</h1>
<ul>
<li>Addition: <code>3 + 4</code></li>
<li>Subtraction: <code>3 - 1</code></li>
<li>Multiplication: <code>4 * 5</code></li>
<li>Division: <code>6 / 5</code></li>
<li>Exponentiation: <code>10 ** 2</code></li>
<li>Modulo (remainder): <code>42 % 5</code></li>
</ul>
<p>You can create more complex expressions using parentheses for grouping: <code>(4 + 5) * 2</code></p>
</section>
<section id="string" class="level1">
<h1>String</h1>
<p><code>+</code> works for strings as well:</p>
<p><code>"this" + "that"</code> => <code>"thisthat"</code></p>
</section>
<section id="boolean" class="level1">
<h1>Boolean</h1>
<ul>
<li>Logical and: <code>True & False</code> or <code>True and True</code>
<ul>
<li>return <code>True</code> only if both operands are <code>True</code></li>
</ul></li>
<li>Logical or: <code>False | True</code> or <code>False or False</code> (not exclusive…)
<ul>
<li>return <code>True</code> if <em>at least</em> one of operands is <code>True</code></li>
</ul></li>
</ul>
<p>Predict what these evaluate to: <code>True and False</code>, <code>True and True</code>, <code>False and False</code>, <code>True or False</code>, <code>True or True</code>, <code>False or False</code></p>
</section>
<section id="blurring-the-lines-with-booleans" class="level1">
<h1>Blurring the lines with booleans</h1>
<ul>
<li>You can also treat <code>True</code> and <code>False</code> like ints <code>1</code> and <code>0</code>, respectively, and vice versa (try <code>True + True</code>)</li>
<li>Scalars other than <code>0</code>, <code>""</code>, <code>False</code> or <code>None</code> will be treated as <code>True</code> in logical expression (“truthy”)</li>
<li>Empty collections will be treated as “falsey” in logical expressions, all other collections truthy</li>
</ul>
</section>
<section id="variables" class="level1">
<h1>Variables</h1>
<p>Like in the shell, we can give things names</p>
<!--Next time around should focus on seqrecord examples instead of person examples from here on out.-->
<!--```python-->
<!--seqname = "BG234"-->
<!--seq = "AGCTAGCT"-->
<!--seqrecord = {"seqname": seqname, "seq": seq}-->
<!--```-->
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">age <span class="op">=</span> <span class="dv">42</span>
name <span class="op">=</span> <span class="st">"Bob Jones"</span>
person <span class="op">=</span> {<span class="st">"name"</span>: name, <span class="st">"age"</span>: age}
<span class="co"># We can use variables just as though they were their values</span>
age <span class="op">/</span> <span class="dv">4</span></code></pre></div>
</section>
<section id="variable-name-rules" class="level1">
<h1>Variable name rules</h1>
<ul>
<li>Must start with an alphabetical character or an underscore (<code>_</code>)</li>
<li>Can contain alphabetical characters, numbers, and underscores; nothing else</li>
<li>Convention is to use all lower case, and separate words with underscores</li>
</ul>
<p>Example: <code>bobs_occupation = "haxxor"</code></p>
</section>
<section id="collection-operations" class="level1">
<h1>Collection operations</h1>
<p>We’ll mostly focus on lists and dictionaries</p>
</section>
<section id="lists" class="level1">
<h1>Lists</h1>
<p>Writing <code>xs = [1, 2, 3, 4, 5, 6, 7]</code> defines a list <code>xs</code>.</p>
<ul>
<li>Concatenation: <code>+</code>
<ul>
<li>(<code>[1, 2] + [3, 4]</code> => <code>[1, 2, 3, 4]</code>)</li>
</ul></li>
<li>Accessing: <code>xs[4]</code>
<ul>
<li>Get the 5th thing in the list</li>
</ul></li>
<li>Changing: <code>xs[3] = 999</code>
<ul>
<li>Change the 4th thing in the list</li>
</ul></li>
</ul>
<p>Note: Python has 0-based indexing.</p>
</section>
<section id="dictionaries" class="level1">
<h1>Dictionaries</h1>
<p>These work similarly to lists, but we have more flexibility over the indices:</p>
<ul>
<li>Get the value: <code>person["name"]</code></li>
<li>Set the value: <code>person["occupation"] = "haxxor"</code></li>
</ul>
<p>Here <code>"occupation"</code> is the <em>key</em>, and <code>"haxxor"</code> is the <em>value</em>. Together, they form a <em>key-value pair</em>.</p>
</section>
<section id="dictionaries---arbitrary-keys" class="level1">
<h1>Dictionaries - arbitrary keys</h1>
<p>Keys are often strings, but don’t have to be:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">crazy_dict <span class="op">=</span> {<span class="dv">4</span>: <span class="st">"some string"</span>, (<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>): <span class="dv">999</span>}
crazy_dict[<span class="dv">4</span>]
crazy_dict[(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>)]</code></pre></div>
<p>They can be numbers, tuples or anything <em>hashable</em>:</p>
<ul>
<li>Most immutable data is hashable.</li>
<li>You can tell whether data pointed to by variable <code>x</code> is hashable by running <code>hash(x)</code></li>
<li>Lists and dictionaries are not hashable.</li>
</ul>
</section>
<section id="data-representation-exercises" class="level1">
<h1>Data representation exercises</h1>
</section>
<section id="what-kind-of-data-is-this" class="level1">
<h1>What kind of data is this?</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">(<span class="st">"this"</span>, <span class="dv">1234</span>)</code></pre></div>
<p>Is it hashable?</p>
</section>
<section id="how-about-this" class="level1">
<h1>How about this?</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">(<span class="dv">4545</span>, [<span class="st">"a"</span>, <span class="st">"b"</span>])</code></pre></div>
<p>Is it hashable?</p>
</section>
<section id="how-would-you-represent" class="level1">
<h1>How would you represent</h1>
<p>A single DNA sequence?</p>
<p><br/></p>
<section id="section" class="level2">
<h2></h2>
<p>(press down for answer)</p>
</section>
<section id="answer" class="level2">
<h2>Answer</h2>
<p>String</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">seq <span class="op">=</span> <span class="st">"AGCTAGCTACGT"</span></code></pre></div>
</section>
</section>
<section id="how-would-you-represent-1" class="level1">
<h1>How would you represent</h1>
<p>A DNA sequence, together with sequence name and other metadata?</p>
<p><br/></p>
<section id="section-1" class="level2">
<h2></h2>
<p>(press down for answer)</p>
</section>
<section id="answer-1" class="level2">
<h2>Answer</h2>
<p>Dictionary</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">seqrecord <span class="op">=</span> {<span class="st">"seq"</span>: <span class="st">"AGCTAGCTACGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG234"</span>}</code></pre></div>
</section>
</section>
<section id="how-would-you-represent-2" class="level1">
<h1>How would you represent</h1>
<p>A collection of DNA sequences and names?</p>
<p><br/></p>
<section id="section-2" class="level2">
<h2></h2>
<p>(press down for answer)</p>
</section>
<section id="answer-2" class="level2">
<h2>Answer</h2>
<p>Dictionary</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">seqrecords <span class="op">=</span> [{<span class="st">"seq"</span>: <span class="st">"AGCTAGCTACGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG234"</span>},
{<span class="st">"seq"</span>: <span class="st">"AGCTTCCCACGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG235"</span>},
{<span class="st">"seq"</span>: <span class="st">"AGATTCCTCCGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG236"</span>}]</code></pre></div>
</section>
</section>
<section id="how-would-you-represent-3" class="level1">
<h1>How would you represent</h1>
<p>A collection of DNA sequences and names, together with metadata about the collection (sampling date and such)?</p>
<p><br/></p>
<section id="section-3" class="level2">
<h2></h2>
<p>(press down for answer)</p>
</section>
<section id="answer-3" class="level2">
<h2>Answer</h2>
<p>Dictionary</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">{<span class="st">"seqrecords"</span>: [{<span class="st">"seq"</span>: <span class="st">"AGCTAGCTACGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG234"</span>},
{<span class="st">"seq"</span>: <span class="st">"AGCTTCCCACGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG235"</span>},
{<span class="st">"seq"</span>: <span class="st">"AGATTCCTCCGT"</span>, <span class="st">"name"</span>: <span class="st">"MBG236"</span>}],
<span class="st">"sampling_location"</span>: <span class="st">"Bangladesh"</span>,
<span class="st">"technician"</span>: <span class="st">"Bob Jones"</span>}</code></pre></div>
</section>
</section>
<section id="functions" class="level1">
<h1>Functions</h1>
<p><em>In which from our rules of logic we compose spells</em></p>
</section>
<section id="writing-a-simple-function" class="level1">
<h1>Writing a simple function</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> square(n):
ans <span class="op">=</span> n <span class="op">*</span> n
<span class="cf">return</span> ans
square(<span class="dv">4</span>)</code></pre></div>
<ul>
<li>When we call the function with the argument <code>4</code>, that value gets passed in for the variable <code>n</code> in the <em>body</em> of the function.</li>
<li>To get a value out of the function we must use <code>return</code></li>
<li>Python uses 4 spaces of indentation to separate what’s in the body of the function from what’s not
<ul>
<li>In other languages, indentation is just “good practice” for readability; Python enforces it as part of the <em>syntax</em></li>
</ul></li>
</ul>
</section>
<section id="other-things-about-functions" class="level1">
<h1>Other things about functions</h1>
<p>Functions are “first class”, in that they can be passed around as data just like numbers, strings, etc.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># Using our square function as a value</span>
<span class="bu">print</span> square
[square, <span class="st">"data"</span>]</code></pre></div>
<p>We’ll see how this is useful later.</p>
</section>
<section id="type-functions" class="level1">
<h1>“Type functions”</h1>
<p>Some types have functions associated with them, such as <code>str.upper</code> and <code>list.append</code>:</p>
<pre><code>str.upper("this")
xs = [1, 2, 3]
list.append(xs, 9)
print xs</code></pre>
<p>You can see what functions are associated with a type <code>t</code> using <code>dir(t)</code> (example: <code>dir(list)</code> shows all the list functions)</p>
</section>
<section id="control-flow" class="level1">
<h1>Control flow</h1>
<p><em>In which we gain mastery over the rules of logic</em></p>
</section>
<section id="if-statements" class="level1">
<h1>If statements</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">x <span class="op">=</span> <span class="dv">3</span>
<span class="cf">if</span> x <span class="op"><</span> <span class="dv">4</span>:
<span class="bu">print</span> <span class="st">"You should see this"</span>
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"You should NOT see this!"</span></code></pre></div>
<p>Things to note:</p>
<ul>
<li>We used indentation here as well to determine the structure of the statements</li>
<li>We used the <code><</code> (less than) operand, which behaves just as you’d expect, returning <code>True</code>/<code>False</code> depending on the operand values</li>
</ul>
</section>
<section id="you-can-have-elif-sections-too" class="level1">
<h1>You can have elif sections too</h1>
<p><code>elif</code> is a combination of an <code>else</code> and an <code>if</code>.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> f(n):
<span class="cf">if</span> n <span class="op"><</span> <span class="dv">5</span>:
<span class="bu">print</span> <span class="st">"Cond 1"</span>
<span class="cf">elif</span> n <span class="op">></span> <span class="dv">10</span>:
<span class="bu">print</span> <span class="st">"Cond 2"</span>
<span class="cf">else</span>:
<span class="bu">print</span> <span class="st">"Cond 3"</span>
f(<span class="dv">3</span>)
f(<span class="dv">7</span>)
f(<span class="dv">12</span>)</code></pre></div>
</section>
<section id="about-collections-again" class="level1">
<h1>About collections again</h1>
<p>What do you think will happen below?</p>
<pre><code>data = []
if data:
print "catgifs"
list.append(data, "puppies?")
if data:
print "yay internetz!"</code></pre>
</section>
<section id="for-statements" class="level1">
<h1>For statements</h1>
<p>Do something for every thing in a collection:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> n <span class="kw">in</span> xs:
<span class="bu">print</span> <span class="st">"On int:"</span>, n</code></pre></div>
</section>
<section id="iterating-over-key-value-pairs-in-a-dictionary" class="level1">
<h1>Iterating over key-value pairs in a dictionary</h1>
<p><code>dict</code> has an <code>items</code> function that gives all of the key, value pairs. Try</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">dict</span>.items(person)</code></pre></div>
<p>Now loop over them:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> k, v <span class="kw">in</span> <span class="bu">dict</span>.items(person):
<span class="bu">print</span> <span class="st">"key:"</span>, k, <span class="st">"val:"</span>, v</code></pre></div>
</section>
<section id="problem" class="level1">
<h1>Problem:</h1>
<p>Using just what we’ve learned already, how would you create a list <code>ys</code> that contained the square of every number in the list <code>xs</code>?</p>
</section>
<section id="using-for-to-build-results" class="level1">
<h1>Using <code>for</code> to build results</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">ys <span class="op">=</span> []
<span class="cf">for</span> x <span class="kw">in</span> xs:
y <span class="op">=</span> square(x)
<span class="bu">list</span>.append(ys, y)
<span class="bu">print</span> ys</code></pre></div>
<p>This is <em>imperative</em>; we’re telling the computer <em>how</em> to build <code>ys</code></p>
</section>
<section id="a-better-way" class="level1">
<h1>A better way</h1>
<p>List comprehensions:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">ys <span class="op">=</span> [square(x) <span class="cf">for</span> x <span class="kw">in</span> xs]
<span class="bu">print</span> ys</code></pre></div>
<p>This is more <em>declarative</em>.</p>
</section>
<section id="the-functional-way" class="level1">
<h1>The functional way</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">ys <span class="op">=</span> <span class="bu">map</span>(square, xs)
<span class="bu">print</span> ys</code></pre></div>
<p>Note that we’re passing the function <code>square</code> as an <em>argument</em> to the function <code>map</code>.</p>
</section>
<section id="declarative-is-generally-better" class="level1">
<h1>Declarative is generally better</h1>
<p>List comprehension and functional approaches are more <em>declarative</em>.</p>
<p>We tell the computer <em>what</em> to compute, not <em>how</em> we want it computed. This is usually a good thing (cleaner code; sometimes better performance).</p>
</section>
<section id="higher-level-concerns" class="level1">
<h1>Higher level concerns</h1>
<p><em>In which we rise to the level of systems</em></p>
</section>
<section id="importing-modules" class="level1">
<h1>Importing modules</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> math
<span class="bu">dir</span>(math)
<span class="bu">help</span>(math.log)
math.log(<span class="fl">3.14</span>)</code></pre></div>
<p><code>math</code> is a <em>module</em>. Modules, just like objects, have <em>attributes</em>, and these attributes are typically functions that we can use in our programs (though sometimes they’re just useful data, like <code>math.e</code>).</p>
</section>
<section id="namespaces" class="level1">
<h1>Namespaces</h1>
<p>We also talk about modules as being <em>namespaces</em>. A namespace is just a “path” to some data or function or module. In this case <code>math</code> can also be thought of as a namespace.</p>
<p>Namespaces help us avoid naming conflicts. If we have a function called <code>square</code>, and we import a module like <code>math</code> with a function called <code>square</code>, namespaces let us distinguish between them.</p>
</section>
<section id="installing-your-own-packages-with-pip" class="level1">
<h1>Installing your own packages with pip</h1>
<p>Collections of modules are called <em>packages</em>.</p>
<p>You can install Python packages using the <code>pip</code> command line tool (the “App Store” for Python packages). From your Unix shell:</p>
<div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="ex">pip</span> install --user biopython</code></pre></div>
<p>The <code>--user</code> flag tells pip to install things in your <em>local</em> python lib directory. You would only omit that if you were installing <em>system wide</em> on your own computer.</p>
</section>
<section id="beware-python-3" class="level1">
<h1>Beware Python 3</h1>
<p>We’re using Python 2. Python 3 has been out for YEARS, but adoption has been slow. Eventually, Python 3 will become the system default and more widely used, but till then…</p>
<p>Bottom line: Python 3 is rather different in a number of ways, so be aware.</p>
</section>
<section id="other-things-in-the-python-ecosystem" class="level1">
<h1>Other things in the Python ecosystem</h1>
<ul>
<li>Numpy & Scipy: Some work in the numerical computing and stats directions</li>
<li>Pandas: R like data frames in Python</li>
<li>Jupyter & IPython: Browser-based “notebook” environments for co-locating code, documentation and visualizations
<ul>
<li>Miss out on some of the Unix-centric goodness, but has advantages too</li>
</ul></li>
</ul>
</section>
<section id="resources" class="level1">
<h1>Resources</h1>
<ul>
<li>Codecademy <a href="http://www.codecademy.com/tracks/python">Python course</a> (I highly recommend working through all of this)</li>
<li>Very clear blog post on <a href="http://nedbatchelder.com/text/names.html">how python variables/state work</a></li>
<li><a href="http://pythontutor.com/">Pythontutor</a> - Visualize the execution of a python program</li>
</ul>
<p><a href="http://fredhutchio.github.io/intro-bioinformatics/">Back to homepage</a></p>
</section>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: false,
slideNumber: true,
rollingLinks: false,
// available themes are in /css/theme
theme: Reveal.getQueryHash().theme || 'csmall',
// default/cube/page/concave/zoom/linear/fade/none
transition: Reveal.getQueryHash().transition || 'fade',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML'}
// { src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'reveal.js/plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
Reveal.addEventListener( 'slidechanged', function( event ) {
MathJax.Hub.Rerender();
} );
</script>
</body>
</html>