forked from IQSS/dss-workshops-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkshop.html
More file actions
executable file
·3652 lines (2773 loc) · 126 KB
/
Copy pathworkshop.html
File metadata and controls
executable file
·3652 lines (2773 loc) · 126 KB
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Python: A Hands-on Introduction</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="title" content="Python: A Hands-on Introduction"/>
<meta name="generator" content="Org-mode"/>
<meta name="generated" content="June, 2013"/>
<meta name="author" content="Alex Storer"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
html { font-family: Times, serif; font-size: 12pt; }
.title { text-align: center; }
.todo { color: red; }
.done { color: green; }
.tag { background-color: #add8e6; font-weight:normal }
.target { }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right {margin-left:auto; margin-right:0px; text-align:right;}
.left {margin-left:0px; margin-right:auto; text-align:left;}
.center {margin-left:auto; margin-right:auto; text-align:center;}
p.verse { margin-left: 3% }
pre {
border: 1pt solid #AEBDCC;
background-color: #F3F5F7;
padding: 5pt;
font-family: courier, monospace;
font-size: 90%;
overflow:auto;
}
table { border-collapse: collapse; }
td, th { vertical-align: top; }
th.right { text-align:center; }
th.left { text-align:center; }
th.center { text-align:center; }
td.right { text-align:right; }
td.left { text-align:left; }
td.center { text-align:center; }
dt { font-weight: bold; }
div.figure { padding: 0.5em; }
div.figure p { text-align: center; }
div.inlinetask {
padding:10px;
border:2px solid gray;
margin:10px;
background: #ffffcc;
}
textarea { overflow-x: auto; }
.linenr { font-size:smaller }
.code-highlighted {background-color:#ffff00;}
.org-info-js_info-navigation { border-style:none; }
#org-info-js_console-label { font-size:10px; font-weight:bold;
white-space:nowrap; }
.org-info-js_search-highlight {background-color:#ffff00; color:#000000;
font-weight:bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="org/css/worg.css" />
<script type="text/javascript" src="http://orgmode.org/org-info.js">
/**
*
* @source: http://orgmode.org/org-info.js
*
* @licstart The following is the entire license notice for the
* JavaScript code in http://orgmode.org/org-info.js.
*
* Copyright (C) 2012-2013 Sebastian Rose
*
*
* The JavaScript code in this tag is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. The code is distributed WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* @licend The above is the entire license notice
* for the JavaScript code in http://orgmode.org/org-info.js.
*
*/
</script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
org_html_manager.set("TOC_DEPTH", "3");
org_html_manager.set("LINK_HOME", "");
org_html_manager.set("LINK_UP", "");
org_html_manager.set("LOCAL_TOC", "1");
org_html_manager.set("VIEW_BUTTONS", "0");
org_html_manager.set("MOUSE_HINT", "underline");
org_html_manager.set("FIXED_TOC", "0");
org_html_manager.set("TOC", "1");
org_html_manager.set("VIEW", "info");
org_html_manager.setup(); // activate after the parameters are set
/*]]>*///-->
</script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble">
</div>
<div id="content">
<h1 class="title">Python: A Hands-on Introduction</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 Goals</a>
<ul>
<li><a href="#sec-1-1">1.1 Who am I?</a></li>
<li><a href="#sec-1-2">1.2 What is this page?</a></li>
</ul>
</li>
<li><a href="#sec-2">2 Basic Python</a>
<ul>
<li><a href="#sec-2-1">2.1 Installation</a>
<ul>
<li><a href="#sec-2-1-1">2.1.1 IDE</a></li>
<li><a href="#sec-2-1-2">2.1.2 Wing IDE 101</a></li>
</ul>
</li>
<li><a href="#sec-2-2">2.2 Further Python Resources</a></li>
<li><a href="#sec-2-3">2.3 Diving In</a>
<ul>
<li><a href="#sec-2-3-1">2.3.1 Basic Text Handling</a></li>
<li><a href="#sec-2-3-2">2.3.2 Indexing and Slicing</a></li>
<li><a href="#sec-2-3-3">2.3.3 Including Other Packages</a></li>
<li><a href="#sec-2-3-4">2.3.4 Objects and methods</a></li>
<li><a href="#sec-2-3-5">2.3.5 Defining Functions</a></li>
<li><a href="#sec-2-3-6">2.3.6 Logical Flow</a></li>
<li><a href="#sec-2-3-7">2.3.7 Review</a></li>
</ul>
</li>
<li><a href="#sec-2-4">2.4 For Loops</a>
<ul>
<li><a href="#sec-2-4-1">2.4.1 Example</a></li>
</ul>
</li>
<li><a href="#sec-2-5">2.5 Lists</a></li>
<li><a href="#sec-2-6">2.6 Exercise</a>
<ul>
<li><a href="#sec-2-6-1">2.6.1 Exercise Solutions</a></li>
</ul>
</li>
<li><a href="#sec-2-7">2.7 <code>dict</code> type</a>
<ul>
<li><a href="#sec-2-7-1">2.7.1 Examples</a></li>
<li><a href="#sec-2-7-2">2.7.2 Writing to CSV</a></li>
<li><a href="#sec-2-7-3">2.7.3 A Note on File Objects</a></li>
<li><a href="#sec-2-7-4">2.7.4 Exercise</a></li>
<li><a href="#sec-2-7-5">2.7.5 Exercise Solutions</a></li>
</ul></li>
</ul>
</li>
<li><a href="#sec-3">3 Regular Expressions</a>
<ul>
<li><a href="#sec-3-1">3.1 A first example</a>
<ul>
<li><a href="#sec-3-1-1">3.1.1 What the code does</a></li>
<li><a href="#sec-3-1-2">3.1.2 Different expressions</a></li>
</ul>
</li>
<li><a href="#sec-3-2">3.2 Further examples</a></li>
<li><a href="#sec-3-3">3.3 Other helpful regex tools</a></li>
<li><a href="#sec-3-4">3.4 Exercises</a></li>
<li><a href="#sec-3-5">3.5 Solutions</a></li>
</ul>
</li>
<li><a href="#sec-4">4 Web Sites</a>
<ul>
<li><a href="#sec-4-1">4.1 Example: Egypt Independent / المصري اليوم</a>
<ul>
<li><a href="#sec-4-1-1">4.1.1 Aside: "Brittleness"</a></li>
<li><a href="#sec-4-1-2">4.1.2 Metadata</a></li>
<li><a href="#sec-4-1-3">4.1.3 Body</a></li>
<li><a href="#sec-4-1-4">4.1.4 Scraping Articles</a></li>
<li><a href="#sec-4-1-5">4.1.5 Scraping Exercise!</a></li>
</ul></li>
</ul>
</li>
<li><a href="#sec-5">5 Web scraping technology</a>
<ul>
<li><a href="#sec-5-1">5.1 Package installation</a></li>
<li><a href="#sec-5-2">5.2 Running on a schedule</a></li>
<li><a href="#sec-5-3">5.3 Using an API</a></li>
<li><a href="#sec-5-4">5.4 Web scraping: selenium</a>
<ul>
<li><a href="#sec-5-4-1">5.4.1 Getting Links</a></li>
<li><a href="#sec-5-4-2">5.4.2 Parsing Contents</a></li>
<li><a href="#sec-5-4-3">5.4.3 Saving our scraped information</a></li>
<li><a href="#sec-5-4-4">5.4.4 Exercise!</a></li>
<li><a href="#sec-5-4-5">5.4.5 Solution</a></li>
<li><a href="#sec-5-4-6">5.4.6 Further Topics</a></li>
</ul>
</li>
<li><a href="#sec-5-5">5.5 Web scraping: lxml</a>
<ul>
<li><a href="#sec-5-5-1">5.5.1 Using ElementTree</a></li>
<li><a href="#sec-5-5-2">5.5.2 Using <code>lxml</code></a></li>
<li><a href="#sec-5-5-3">5.5.3 Stripping text</a></li>
<li><a href="#sec-5-5-4">5.5.4 Parallelization to increase speed</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Goals</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>Get a working python environment installed on your own computer
</li>
<li>Make python seem less scary
</li>
<li>Understand some of the differences between python and other languages
</li>
<li>Understand what screen scraping is all about
</li>
<li>Learn the tools to scrape web sites (and other structured text)
effectively
</li>
</ul>
<p>
This may take some time! This time is available indefinitely,
depending on how quickly we go it could take a number of sessions -
my intention is to play it by ear to see what we need to focus on.
</p>
</div>
<div id="outline-container-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Who am I?</h3>
<div class="outline-text-3" id="text-1-1">
<p>
My name is Alex Storer, and I'm part of the <a href="http://dss.iq.harvard.edu">Data Science Services</a> team at IQSS. I have a PhD in Computational Neuroscience,
and have done a lot of programming and scripting to interact with
data.
</p>
<p>
Our team can help you with your research questions, both with the
statistics and the technology. If you want to chat with us, simply
e-mail <a href="#support-help.hmdc.harvard.edu">support@help.hmdc.harvard.edu</a>.
</p>
</div>
</div>
<div id="outline-container-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> What is this page?</h3>
<div class="outline-text-3" id="text-1-2">
<p>
This is a tutorial that I wrote using the org-mode in emacs. It is
hosted here:
</p>
<p>
<a href="http://www.people.fas.harvard.edu/~astorer/scraping/scraping.html">http://www.people.fas.harvard.edu/~astorer/scraping/scraping.html</a>
</p>
<p>
You can always find details about our ongoing workshops here:
</p>
<p>
<a href="http://dss.iq.harvard.edu">http://dss.iq.harvard.edu</a>
</p>
</div>
</div>
</div>
<div id="outline-container-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> Basic Python</h2>
<div class="outline-text-2" id="text-2">
<p>Python is a powerful interpreted language that people often use for
scraping. We'll highlight here a few of the most helpful features for
understanding Python code and writing scrapers. This is by no means a
complete or thorough introduction to Python! It's just enough to get
by.
</p>
</div>
<div id="outline-container-2-1" class="outline-3">
<h3 id="sec-2-1"><span class="section-number-3">2.1</span> Installation</h3>
<div class="outline-text-3" id="text-2-1">
<p> Python comes in two modern flavors, version 2 and version 3. There
are some important language differences between them, and in
practice, almost everyone uses version 2. To install it, go <a href="http://python.org/download/">here</a>
and select the relevant operating system.
</p>
</div>
<div id="outline-container-2-1-1" class="outline-4">
<h4 id="sec-2-1-1"><span class="section-number-4">2.1.1</span> IDE</h4>
<div class="outline-text-4" id="text-2-1-1">
<p> An <b>IDE</b>, or Integrated Development Environment, is used to
facilitate programming. A good IDE does things like code
highlighting, error checking, one-click-running, and easy
integration across multiple files. An example of a crappy IDE is
notepad. I like to use emacs. Most people prefer something else.
</p></div>
</div>
<div id="outline-container-2-1-2" class="outline-4">
<h4 id="sec-2-1-2"><span class="section-number-4">2.1.2</span> Wing IDE 101</h4>
<div class="outline-text-4" id="text-2-1-2">
<p> For this session, I recommend <a href="http://wingware.com/downloads/wingide-101">Wing 101</a>. It's a free version of a
more fully-featured IDE, but for beginners, it's perfect. If you
don't already have an IDE that you're invested in, or you want
your intro to python to be as painless as possible, you should
install it. It's cross platform.
</p><ul>
<li id="sec-2-1-2-1">Getting Started in Wing<br/>
Once you have Wing installed, you might want to use the tutorial
to learn how to navigate around in it.
<div class="figure">
<p><img src="./img/tutorial.jpg" title="Wing Tutorial" align="center" alt="./img/tutorial.jpg" /></p>
<p>Opening the tutorial in Wing 101.</p>
</div>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-2-2" class="outline-3">
<h3 id="sec-2-2"><span class="section-number-3">2.2</span> Further Python Resources</h3>
<div class="outline-text-3" id="text-2-2">
<p>
<b>But wait, I want to spend four months becoming a Python guru!</b>
</p>
<p>
Dude, you're awesome. Here are some resources that will help you:
</p>
<ul class="org-ul">
<li><a href="http://knuth.luther.edu/~leekent/IntroToComputing/">Python Programming Fundamentals</a>
Uses WingIDE to teach basic computer science tactics using python
</li>
<li><a href="http://www.pythonchallenge.com/">Python Challenge</a>
A fun programming riddle that will increase your chops.
</li>
<li><a href="http://learnpythonthehardway.org/">Learn Python The Hard Way</a>
The Hard Way means by actually writing code. Maybe it should be
called The Good Way?
</li>
</ul>
</div>
</div>
<div id="outline-container-2-3" class="outline-3">
<h3 id="sec-2-3"><span class="section-number-3">2.3</span> Diving In</h3>
<div class="outline-text-3" id="text-2-3">
<p> In Wing, there is a window open called <b>Python Shell</b>
</p><ul class="org-ul">
<li>If you know <b>R</b>, think of this just like the R command line
</li>
<li>If you've never programmed before, think of this as a graphing calculator
</li>
</ul>
<pre class="src src-python"><span style="color: #728a05;">print</span> 2+4
</pre>
<pre class="example">
6
</pre>
</div>
<div id="outline-container-2-3-1" class="outline-4">
<h4 id="sec-2-3-1"><span class="section-number-4">2.3.1</span> Basic Text Handling</h4>
<div class="outline-text-4" id="text-2-3-1">
<ul class="org-ul">
<li>Of course, this graphing calculator can handle text, too!
</li>
</ul>
<pre class="src src-python"><span style="color: #2075c7;">mystr</span> = <span style="color: #259185;">"Hello, World!"</span>
<span style="color: #728a05;">print</span> mystr
<span style="color: #728a05;">print</span> <span style="color: #728a05;">len</span>(mystr)
</pre>
<pre class="example">
Hello, World!
13
</pre>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup><col class="left" /><col class="left" /><col class="left" />
</colgroup>
<tbody>
<tr><td class="left"><b>Python Code</b></td><td class="left"><b>R Code</b></td><td class="left"><b>English Translation</b></td></tr>
<tr><td class="left"><code>print 2+4</code></td><td class="left"><code>print(2+4)</code></td><td class="left">Print the value of 2+4</td></tr>
<tr><td class="left"><code>mystr = '`Hello World'`</code></td><td class="left"><code>mystr <- '`Hello World'`</code></td><td class="left">Assign the string "Hello World" to the variable mystr</td></tr>
<tr><td class="left"><code>len(mystr)</code></td><td class="left"><code>nchar(mystr)</code></td><td class="left">How "long" is the variable mystr? <i>Note: R can tell you how long it is, but if you want the number of characters, that's what you need to ask for.</i></td></tr>
</tbody>
</table>
<p>
<b>Note to Stata Users:</b><br/>
Assigning a variable is not the same as adding a "column" to your dataset.
</p>
</div>
</div>
<div id="outline-container-2-3-2" class="outline-4">
<h4 id="sec-2-3-2"><span class="section-number-4">2.3.2</span> Indexing and Slicing</h4>
<div class="outline-text-4" id="text-2-3-2">
<p>Get the first element of a string.
</p>
<ul class="org-ul">
<li><b>Note:</b> Python counts from 0. This is a common convention in most
languages constructed by computer scientists.
</li>
</ul>
<pre class="src src-python"><span style="color: #2075c7;">mystr</span> = <span style="color: #259185;">"Dogs in outer space"</span>
<span style="color: #728a05;">print</span> mystr[0]
</pre>
<pre class="example">
D
</pre>
<p>
Get the last element of a string
</p>
<pre class="src src-python"><span style="color: #2075c7;">mystr</span> = <span style="color: #259185;">"Dogs in outer space"</span>
<span style="color: #728a05;">print</span> mystr[-1]
<span style="color: #728a05;">print</span> mystr[<span style="color: #728a05;">len</span>(mystr)-1]
</pre>
<pre class="example">
e
e
</pre>
<pre class="src src-python"><span style="color: #2075c7;">mystr</span> = <span style="color: #259185;">"Dogs in outer space"</span>
<span style="color: #728a05;">print</span> mystr[1:3]
<span style="color: #728a05;">print</span> mystr[3:]
<span style="color: #728a05;">print</span> mystr[:-3]
</pre>
<pre class="example">
og
s in outer space
Dogs in outer sp
</pre>
</div>
</div>
<div id="outline-container-2-3-3" class="outline-4">
<h4 id="sec-2-3-3"><span class="section-number-4">2.3.3</span> Including Other Packages</h4>
<div class="outline-text-4" id="text-2-3-3">
<ul class="org-ul">
<li>By default, python doesn't include every possible "package"
<ul class="org-ul">
<li>This is similar to R, but unlike Matlab
</li>
<li>Use the <code>include</code> statement to load a library
</li>
</ul>
</li>
</ul>
<pre class="src src-python"><span style="color: #728a05;">import</span> math
<span style="color: #728a05;">print</span> math.sin(math.pi)
</pre>
<pre class="example">
1.22464679915e-16
</pre>
<p>
After we import from a package, we have to access sub-elements of that
package using the <code>.</code> operator. Notice also that while the value
<code>1.22464679915e-16</code> is very nearly <code>0</code>, the <code>math</code> module doesn't know
that sin(π) = 0. There are smarter modules for doing math in
Python, like <code>scipy</code> and <code>numpy</code>. Some people love using Python for
Math. I think it makes more sense to use R.
</p>
<ul class="org-ul">
<li>If you want to <code>import</code> something into your namespace
<ul class="org-ul">
<li><code>from math import <myfunction></code> <b>or</b>
</li>
<li><code>from math import *</code>
</li>
</ul>
</li>
</ul>
<pre class="src src-python"><span style="color: #728a05;">from</span> math <span style="color: #728a05;">import</span> *
<span style="color: #728a05;">print</span> sin(pi)
</pre>
<pre class="example">
1.22464679915e-16
</pre>
</div>
</div>
<div id="outline-container-2-3-4" class="outline-4">
<h4 id="sec-2-3-4"><span class="section-number-4">2.3.4</span> Objects and methods</h4>
<div class="outline-text-4" id="text-2-3-4">
<p>
Python makes extensive use of <b>objects</b>. An object has
</p><ul class="org-ul">
<li>Methods: functions that work only on that option
</li>
<li>Fields: data that only that type of object has
</li>
</ul>
<p>
For example, let's imagine a <code>fruit</code> object. A <code>fruit</code> might have a
field called <code>hasPeel</code>, which tells you whether this fruit is
peeled. It could also have a method called <code>peel</code>, which alters the
state of the fruit.
</p>
<pre class="src src-python"><span style="color: #728a05;">str</span> = <span style="color: #259185;">"THE World is A BIG and BEAUTIFUL place. "</span>
<span style="color: #728a05;">print</span> <span style="color: #728a05;">str</span>.upper()
<span style="color: #2075c7;">name</span> = <span style="color: #259185;">"Alex Storer"</span>
<span style="color: #728a05;">print</span> name.swapcase()
</pre>
<pre class="example">
THE WORLD IS A BIG AND BEAUTIFUL PLACE.
aLEX sTORER
</pre>
<p>
Here we defined two strings, <code>str</code> and <code>name</code>, and used these to
invoke string methods which affect the case of the string.
</p>
<ul class="org-ul">
<li>You can write your own objects and methods
</li>
<li>Objects can be sub-classes of other objects
<ul class="org-ul">
<li>e.g., a <code>psychologist</code> is a type of <code>researcher</code>, who does
everything a <code>researcher</code> does but also some other things only
a <code>pyschologist</code> does.
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-2-3-5" class="outline-4">
<h4 id="sec-2-3-5"><span class="section-number-4">2.3.5</span> Defining Functions</h4>
<div class="outline-text-4" id="text-2-3-5">
<p>
You can write your own functions, pieces of code that can be used to
take specific inputs and give outputs. You can create a function by
using the <code>def</code> command.
</p>
<pre class="src src-python"><span style="color: #728a05;">def</span> <span style="color: #2075c7;">square</span>(x):
<span style="color: #728a05;">return</span> x*x
<span style="color: #728a05;">print</span> square(9)
</pre>
<pre class="example">
81
</pre>
<p>
Pay close attention to the <b>whitespace</b> that is used in Python!
Unlike other languages, it is not ignored. Everything with the same
indentation is in the same level. Above, the statement <code>return x*x</code>
is part of the <code>square</code> function, but the following line is outside of
the function definition.
</p>
</div>
</div>
<div id="outline-container-2-3-6" class="outline-4">
<h4 id="sec-2-3-6"><span class="section-number-4">2.3.6</span> Logical Flow</h4>
<div class="outline-text-4" id="text-2-3-6">
<div class="figure">
<p><img src="./img/decision-tree.png" alt="./img/decision-tree.png" /></p>
<p>The xkcd guide to writing good code</p>
</div>
<p>
You can think about this logical process as being in pseudocode.
</p>
<pre class="example">
IF do things right
---> code well
OTHERWISE
---> do things fast
</pre>
<p>
A lot of programming is figuring out how to fit things into this sort
of <code>if=/=else</code> structure. Let's look at an example in Python.
</p>
<ul class="org-ul">
<li>The method <code>find</code> returns the index of the first location of a
string match
</li>
</ul>
<pre class="src src-python"><span style="color: #2075c7;">mystr</span> = <span style="color: #259185;">"This is one cool looking string!"</span>
<span style="color: #728a05;">if</span> mystr.find(<span style="color: #259185;">"string"</span>)><span style="color: #728a05;">len</span>(mystr)/2:
<span style="color: #728a05;">print</span> <span style="color: #259185;">"The word 'string' is in the second half"</span>
<span style="color: #728a05;">else</span>:
<span style="color: #728a05;">print</span> <span style="color: #259185;">"The word 'string is not in the second half"</span>
</pre>
<pre class="example">
The word 'string' is in the second half
</pre>
<p>
What happens if the word "string" is not there at all?
</p>
<ul class="org-ul">
<li>The method <code>find</code> returns -1 if the string isn't found
</li>
</ul>
<pre class="src src-python"><span style="color: #2075c7;">mystr</span> = <span style="color: #259185;">"I don't know about you, but I only use velcro."</span>
<span style="color: #728a05;">print</span> mystr.find(<span style="color: #259185;">"string"</span>)
<span style="color: #728a05;">if</span> mystr.find(<span style="color: #259185;">"string"</span>)><span style="color: #728a05;">len</span>(mystr)/2:
<span style="color: #728a05;">print</span> <span style="color: #259185;">"The word 'string' is in the second half"</span>
<span style="color: #728a05;">elif</span> mystr.find(<span style="color: #259185;">"string"</span>)>=0:
<span style="color: #728a05;">print</span> <span style="color: #259185;">"The word 'string is not in the second half"</span>
<span style="color: #728a05;">else</span>:
<span style="color: #728a05;">print</span> <span style="color: #259185;">"The word 'string' isn't there!"</span>
</pre>
<pre class="example">
-1
The word 'string' isn't there!
</pre>
<ul class="org-ul">
<li><b>Important Note:</b> In Python, most everything evaluates to <code>True</code>.
Exceptions include <code>0</code> and <code>None</code>. This means that you can say
things like <code>if (result)</code> where the result may be a computation, a
string search, or anything like that. As long as it evaluates to
<code>True</code>, it will work!
</li>
</ul>
</div>
</div>
<div id="outline-container-2-3-7" class="outline-4">
<h4 id="sec-2-3-7"><span class="section-number-4">2.3.7</span> Review</h4>
<div class="outline-text-4" id="text-2-3-7">
<ul class="org-ul">
<li><code>if</code>, <code>elif</code> and <code>else</code> can be used to control the flow of a program
</li>
<li>strings are a type of a object, and have a number of methods that
come with them, including <code>find</code>, <code>upper</code> and <code>swapcase</code>
<ul class="org-ul">
<li>methods are called using <code>mystring.method()</code>
</li>
<li>The list of methods for strings can be found in the <a href="http://docs.python.org/library/stdtypes.html">Python documentation</a>
</li>
</ul>
</li>
<li><code>def</code> can be used to define a function
<ul class="org-ul">
<li>The <code>return</code> statement determine what the function returns
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-2-4" class="outline-3">
<h3 id="sec-2-4"><span class="section-number-3">2.4</span> For Loops</h3>
<div class="outline-text-3" id="text-2-4">
<p> The for loop is a major component of how python is used. You can
iterate over lots of different things, and python is smart enough
to know how to do it.
</p><ul class="org-ul">
<li><b>Note:</b> the following is what's called pseudocode - something
that looks like code, but isn't going to run. It's a helpful way
to clarify the steps that you need to take to get things to
work.
</li>
</ul>
<pre class="src src-python"><span style="color: #728a05;">for</span> (item <span style="color: #728a05;">in</span> container):
process item
<span style="color: #728a05;">print</span> item
<span style="color: #728a05;">print</span> <span style="color: #259185;">"done processing items!"</span>
</pre>
<p>
Notice the use of the <TAB> (or spacing) - that's how python knows whether we're
inside the loop or not!
</p>
</div>
<div id="outline-container-2-4-1" class="outline-4">
<h4 id="sec-2-4-1"><span class="section-number-4">2.4.1</span> Example</h4>
<div class="outline-text-4" id="text-2-4-1">
<pre class="src src-python"><span style="color: #728a05;">str</span> = <span style="color: #259185;">"Daddy ran to help Ann. Up and down went the seesaw."</span>
<span style="color: #728a05;">for</span> word <span style="color: #728a05;">in</span> <span style="color: #728a05;">str</span>.split():
<span style="color: #728a05;">print</span> word
</pre>
<pre class="example">Daddy
ran
to
help
Ann.
Up
and
down
went
the
seesaw.
</pre>
<p>
Notice the use of <code>str.split()</code>: this is an example of calling a
<i>method</i> of a <i>string object</i>. It returns a <b>list</b> of words after
splitting the string on whitespace.
</p>
</div>
</div>
</div>
<div id="outline-container-2-5" class="outline-3">
<h3 id="sec-2-5"><span class="section-number-3">2.5</span> Lists</h3>
<div class="outline-text-3" id="text-2-5">
<ul class="org-ul">
<li>A <b>list</b> is a data type that can hold anything.
</li>
<li>Lists are iterable (you can pass them to a <code>for</code> loop
</li>
<li>You can <code>.append</code>, <code>.extend</code>,and otherwise manipulate lists. <a href="http://docs.python.org/tutorial/datastructures.html">Python Documentation</a>
</li>
</ul>
<pre class="src src-python"><span style="color: #2075c7;">mylist</span> = [<span style="color: #259185;">'dogs'</span>,1,4,<span style="color: #259185;">"fishes"</span>,[<span style="color: #259185;">"hearts"</span>,<span style="color: #259185;">"clovers"</span>],<span style="color: #728a05;">list</span>]
<span style="color: #728a05;">for</span> element <span style="color: #728a05;">in</span> mylist:
<span style="color: #728a05;">print</span> element
mylist.reverse()
<span style="color: #728a05;">print</span> mylist
</pre>
<pre class="example">
dogs
1
4
fishes
['hearts', 'clovers']
<type 'list'>
[<type 'list'>, ['hearts', 'clovers'], 'fishes', 4, 1, 'dogs']
</pre>
</div>
</div>
<div id="outline-container-2-6" class="outline-3">