-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathslides.html
1259 lines (944 loc) · 43.4 KB
/
slides.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Ladies Learning Code</title>
<!-- Don't alter slideshow.css, CSSS slide deck needs it to work -->
<link rel="stylesheet" href="framework/css/slideshow.css" data-noprefix>
<!-- Theme-specific styles -->
<link href='http://fonts.googleapis.com/css?family=Quicksand%7CPacifico%7COpen+Sans:400italic,400,300' rel='stylesheet' type='text/css' data-noprefix>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="framework/css/highlightjs-themes/github.css" data-noprefix>
<link rel="stylesheet" href="framework/css/theme-llc.css" data-noprefix>
<link rel="shortcut icon" href="framework/img/favicon.ico">
<!-- Workshop-specific styles -->
<link rel="stylesheet" href="framework/css/workshop.css" data-noprefix>
<!-- Takes care of CSS3 prefixes! -->
<script src="framework/scripts/prefixfree.min.js"></script>
<!-- opens all links in a new window -->
<base target="_blank">
</head>
<!-- Timer/progress bar: Define the presentation duration using "data-duration" in minutes. -->
<body data-duration="360">
<!-- Welcome slide -->
<header class="slide intro first">
<img class="logo" src="framework/img/logo-llc-white.png" alt="Ladies Learning Code logo">
<div class="info">
<h1 class="heading-bg">
<span class="dark">Welcome to</span><br>
<span>Ladies Learning Code!</span>
</h1>
<!-- TODO: Fill in WiFi details -->
<!-- <div class="heading-bg connected">
<span class="dark">GET CONNECTED:</span>
<span class="light">NETWORK: network name</span><br>
<span class="light">PASSWORD: thepassword</span>
</div> -->
</div>
<div class="sponsor">
<p>In partnership with:</p>
<img src="framework/img/telus-logo-white.svg" alt="Telus logo">
</div>
</header>
<main>
<!-- Instructor info -->
<section class="slide intro">
<img class="logo" src="framework/img/logo-llc-white.png" alt="Ladies Learning Code logo">
<div class="info">
<h1 class="heading-bg">
<span>Fundamentals of Programming:</span><br>
<span>Introduction to Python</span>
</h1>
<!-- TODO: Fill in instructor details -->
<!-- <h2><span>with</span> Instructor Name</h2>
<img class="instructor-img" src="http://cl.ly/image/0r3Q3H110G36/profile-generic.jpg" alt="instructor name">
<ul>
<li><i class="fa fa-envelope"></i><a href="mailto:">[email protected]</a></li>
<li><i class="fa fa-desktop"></i><a href="#">mywebsite.com</a></li>
<li><i class="fa fa-twitter"></i><a href="http://twitter.com/">@twitter</a></li>
</ul> -->
</div>
</section>
<section class="slide project" data-markdown>
<script type="text/template">
# Today’s project
Imagine you’re organizing **National Learn to Code Day 2015**.
![](framework/img/workshop/learn-to-code-day.jpg) <!-- .element: class="col-2" -->
You want it to be an *even bigger success* than last year, so not only do you need to know how many people attended (so you can beat that target), but you also want to pull other salient stats to put in a press release.
News outlets *love* stats.
<!-- Computers are very good at repetitive tasks, but they require *precise instructions*. -->
Today, you’ll learn to 'think' like a computer as you gather insights into who attended Ladies Learning Code workshops in 2014.
<div class="presenter-notes">
Whether you take these tools and apply them to your own data or keep using the tools you know, by the end of the day, you’ll hopefully have greater confidence in leveraging computers to do your work faster and focus on what matters to you.
</div>
</script>
</section>
<section class="slide agenda" data-markdown>
<script type="text/template">
## Agenda
###Morning
* Types
* Variables
* Conditionals & Logic
* Reading a file
* For loops
###Afternoon
* Libraries
* Dictionaries
* Finding & countings patterns
* Final Project
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## What is programming?
* Programming is giving instructions to your computer to get it to do things for you
* Computers need *very specific* instructions
* We use a programming language to give the computer instructions in a form that it can understand
* Programming languages have syntax, just like human languages do
<br>
## Why Python Is Awesome
* Free
* Open source
* Write once, run anywhere
* Great community
* The syntax looks like English
</script>
</section>
<section class="slide who-uses" data-markdown>
<script type="text/template">
## Who uses Python?
* Data Analysis
* Scientific Computing
* Scripting
* Testing
* Web Applications (Django, Flask)
* and some of your favorite websites!
<!-- * Youtube, Reddit, Pinterest, Instagram, Dropbox, The Onion -->
![](framework/img/workshop/logo-dropbox.png)
![](framework/img/workshop/logo-instagram.png)
![](framework/img/workshop/logo-pinterest.png)
![](framework/img/workshop/logo-youtube.png)
![](framework/img/workshop/logo-theonion.png)
</script>
</section>
<section class="slide centered" data-markdown>
<script type="text/template">
</br></br> </br>
![Kind of a big deal](http://31.media.tumblr.com/97e5702f9946a834c6288de14d080024/tumblr_miinvvJM9j1s410g9o1_500.gif) <!-- .element: style="width: 50%" -->
</script>
</section>
<section class="slide title" data-markdown>
<script type="text/template">
# Let’s get started!
</script>
</section>
<section class="slide" data-markdown>
## Development Environment
You will need:
1. **Python 2.7.x installed** ( [Mac](https://www.python.org/downloads/), [Windows](https://www.python.org/downloads/windows/) )
1. **[Pycharm Educational Edition](https://www.jetbrains.com/pycharm-educational/)**
1. **[Google Chrome](https://www.google.com/intl/en-CA/chrome/browser/)** - Choose a modern web browser that supports current web standards and has good development tools. To set Chrome as your default browser, follow [these instructions](https://support.google.com/chrome/answer/95417?hl=en).
(Visit [browsehappy.com](http://browsehappy.com) for more info about modern browsers.)
1. Learner files from LLC (containing the exercise and reference files).
</section>
<section class="slide centered" data-markdown>
<script type="text/template">
## PyCharm IDE
IDEs (Integrated Development Environments) help us write code faster with fewer mistakes.
</br></br></br>
![Yes! That is awesome!](http://33.media.tumblr.com/01744bb3d8772b180de821b6cb9e31f1/tumblr_mormzpYsBZ1s410g9o2_400.gif) <!-- .element: style="width: 60%" -->
</script>
</section>
<section class="slide side-by-side" data-markdown>
<script type="text/template">
## Setting up your project files
Open PyCharm and select **Open**. Then navigate to the **llc-intro-to-python-master** folder & select **Choose**.
![](framework/img/workshop/pycharm-project1.png)
![](framework/img/workshop/pycharm-project2.png)
The project files contained within the folder will now appear in your sidebar. Select the arrows to open the folders within. Today, you will be working from the **exercises** folder. The **reference** folder contains today’s project files for reference as an answer key.
The other folders & files are needed to make this slide presentation work so avoid making edits to them!
![](framework/img/workshop/pycharm-project3.png)
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Running Python in PyCharm
The console is an interactive tool used to test and debug code and it’s included in PyCharm!
Let’s open the console:
![Tools > Python Console…](./framework/img/workshop/open-console1.png)
The console will pop up at the bottom of the application. Python is now ready to do your bidding...
![Python Console in PyCharm](./framework/img/workshop/open-console2.png)
Now you can type code after the green `>>>` symbol. It is used by the console to represent a space for you to enter your input but you don’t have to actually type it. You can also open and close the console by selecting the icon in the lower right of PyCharm.
</script>
</section>
<!-- -------------------- -->
<!-- First Steps -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Python can do math
In the console, after the `>>>`, try out these examples.
The command you input after the `>>>` gets *evaluated* and a value is returned back to you.
```
>>> 2 + 2
4
```
```
>>> 2 * 15
30
```
```
>>> 1 / 2
0
```
Why is `1 / 2 = 0`? We’ll come back to that.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Data Types
We just used the *integer* number **type** but there are many more.
* Integer (`int`, whole numbers)
* Float (numbers containing decimals)
* String (`str`, words, sentences, literal text)
* Boolean (true, false)
* list [and more!](http://www.diveintopython3.net/native-datatypes.html)
* Use `type()` and a data value inside the parentheses to figure out the type.
```
>>> type("hello")
<type 'str'>
>>> type(42)
<type 'int'>
>>> type(3.14159)
<type 'float'>
>>> type([1, 2, 3, 4])
<type 'list'>
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Printing
The `print()` **function** outputs values to the screen.
Functions at its most basic definition holds a set of instructions. More on functions later.
Also, note the usage of quotes.
```
>>> print("Hello World!")
Hello World!
```
```
>>> print(2 + 2)
4
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Concatenation
You can "add" strings together, and that’s called **concatenation**. It joins multiple strings into one.
```
>>> print("Hello " + "World!")
Hello World!
```
But what if we try to join a string and a number?
```python
>>> print(2 + "2")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Why did that error happen?
Python needs to know what **type** something is to figure out how to work with it. Let’s look at that error again.
```python
>>> print(2 + "2")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
```
* The first `2`, inside of `print(2 + "2")` is an integer.
* `"2"` enclosed in the quotes, is a **string** type, or "literal text".
* Python can’t figure out how to add a number and string, so it spits out an error.
</script>
</section>
<!-- -------------------- -->
<!--Typecasting-->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Typecasting
We can tell Python how to convert that `"2"` into a number and **change the type**. In this case, change the string to an integer (whole number) using the `int()` function. Try it in your console.
```
>>> print(2 + int("2"))
4
```
<br>
We can also fix the `1/2 = 0` error in the previous example.
When Python divides two integers, it doesn’t know that the answer isn’t an integer so it gets confused.
Integer **types** don’t have decimal parts, so we have to convert to a more precise type, in this case **floats** since this number **type** can handle decimals.
```
>>> print(float(1) / float(2))
0.5
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Typecasting (Continued)
Python can also be used to convert numbers to strings.
```
>>> print(99 + " bottles of beer on the wall")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
```
Use the `str()` function to convert a number to a string.
```
>>> print(str(99) + " bottles of beer on the wall")
```
The ability to tell Python to change the type will come in handy later.
</script>
</section>
<!-- -------------------- -->
<!--Variables-->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Variables
* In programming, we usually want to store a value to use later
* Variables store data in your computer’s memory and you use a **variable name** to fetch their value (kind of like a bookmark in a book)
* Use the equal (`=`) symbol to assign a value to a variable
```python
>>> x = 5
>>> y = 4
>>> x * y
20
```
```python
>>> today = "Saturday"
>>> print("Today is " + today)
Today is Saturday
```
You can store almost anything in a variable.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Reassigning Variables
Variables can be changed after you’ve assigned the value by "reassigning" them.
```python
# Remember, we've already set x = 5 and y = 4
>>> y = 10
>>> x * y
```
What do you think the value of `x * y` will be? <span class="delayed">50</span>
Why?
You changed <!-- .element: class="delayed" -->`y`, but the old value of `x` is still in memory.
Python will remember variable names until you close PyCharm or [clear them manually](https://docs.python.org/2/tutorial/datastructures.html#the-del-statement).
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Pro Tip! Use Comments
* Programmers use *comments* to leave notes for themselves (and other people reading their code in the future)
* Good comments add information that isn’t immediately obvious
* You’ll see more comments throughout the exercises
* Feel free to add your own!
```
# Python will ignore anything after the # symbol
```
</script>
</section>
<!-- -------------------- -->
<!-- Exercise 1: Hello World -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Class Exercise 1: Hello World
We’ve already used the console to put a 'Hello World' message on the screen, so why not take it one step further and have Python say hello to anyone?
Python has a `raw_input()` function that gets data typed by a user and **returns** that value to you.
1. You give `raw_input()` a message to let your user know what you’re looking for
1. The user types something and hits the **enter** key
1. Whatever the user types is **returned** to you.
Let’s try it in your console:
```python
>>> raw_input("What's your name?")
```
It outputs `What's your name?`
You type in your name, press **enter**, and it will print it out on the next line.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Class Exercise 1: Hello World (part 2)
Typing into the console is fine to start, but do you really want to type your program every time you run it?
Python can run your code from a file with a `.py` **extension**
*(<!-- .element: class="footnote" -->the letters after `.` in a filename tell your computer which program can run it)*
Let’s create our first Python file!
1. Right-click on the **exercises** folder to create a new file.
Then select **New > Python File**
![Create a new Python File](./framework/img/workshop/new-file-mac.png)
1. Enter the name `ex1_hello_world.py` in the popup box.
![new python file name](framework/img/workshop/new-python-file.png)
1. Now we can add our simple program to the file
```python
# Assign the raw_input value to a variable
# Your program waits for the user to hit enter
name = raw_input("Please enter your name: ")
# … then continues to this line
print("Hello " + name)
```
Now how do we get PyCharm to run this file?
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Class Exercise 1: Hello World (part 3)
Code in a file is just text until you **run** it through the Python program.
1. To run the code saved in your file, select **Run > Run…**
![](framework/img/workshop/pycharm-run.png)
*<!-- .element: class="footnote" -->Note: PyCharm automatically saves your file before you run it.*
1. Click on `ex1_hello_world` (again) in the menu popup that appears in the middle of the screen.
![Click to run your program](./framework/img/workshop/run-mini-menu.png)
1. Now click into the console window that opened, type your name and press the **Enter** key. You should see your name repeated back at you.
![Demo of hello_world example](./framework/img/workshop/ex1-hello-world.png)
1. You can run your program as many times as you like by repeating the steps above.
</script>
</section>
<!-- -------------------- -->
<!-- Conditionals & Booleans -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Conditionals & Logic
* The program we just wrote ran straight from top to bottom
* What if the program could act differently and decide what to do?
* Use conditionals like an `if` statement
* Each `if` statement contains a test, and the indented block is the code that will run if the test is `True` but not if it is `False` and looks something like this:
```python
# Not real code
if it is cold outside:
wear a sweater
else:
dress normally :)
```
You can also have more than one condition.
```python
# Still not real code
if it is cold outside:
wear a sweater
elif it is raining:
bring an umbrella
else:
dress normally :)
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Boolean values
How does `if` know what’s `True` or `False`? We use 'Boolean' statements to test against values.
Reminder, one equals symbol only *assigns* a value to the variable.
```python
>>> weather = "raining"
```
Test the values. The double equals symbol (`==`) will evaluate if the value on the right matches the assigned value.
```
>>> weather == "cold"
False
>>> weather == "raining"
True
```
We can use these *statements* with our `if` values to test for our weather conditions
</script>
</section>
<!-- -------------------- -->
<!-- Exercise 2: Weather -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Class Exercise 2: Weather
Create a new file `ex2_weather_advice.py` and write out the code snippet below (avoid the temptation of copy-paste).
Note the syntax and **indentation** (whitespace is very important in Python). Try to understand each line as you type it.
```python
# file: ex2_weather_advice.py
weather = raw_input("What is the weather? (cold, raining, etc.)")
if weather == "cold":
print("Wear a sweater!")
elif weather == "raining":
print("Bring an umbrella")
else:
print("Dress normally :)")
```
Try running your file a few times to get different advice (the weather is so unpredictable!)
**Bonus: ** Update the conditions or add more conditions.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Indentation
Python uses indentation to figure out which 'block' of code belongs inside the `if` or `else`.
When you're typing out examples that use an `if` or `else` in the console, you'll see this:
```python
>>> if weather == "cold":
... ̺̺̺̺
```
The `...` and auto-indentation is the console signalling that it expects the nested block code.
When you're done, press enter twice (or backspace and type `else:` and you'll get your next indented block).
```python
>>> if weather == "cold":
... print("Wear a sweater!")
... else:
... print("Dress normally :)")
File "<input>", line 5
print("Dress normally :)")
^
IndentationError: expected an indented block
```
</script>
</section>
<!-- -------------------- -->
<!-- More conditions -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Testing numbers with Math Comparison Operators
Just as you could check the exact value of a string or number you can use typical math operators to check relative numeric values
`>`: Greater than
`<`: Less than
`>=`: Greater than or equal to
`<=`: Less than or equal to
```python
>>> x = 5
>>> print(x > 5)
False
>>> print(x >= 5)
True
>>> if x >= 5:
... print(x + " is greater than or equal to 5")
... else:
... print(x + " is less than 5")
...
5 is greater than or equal to 5
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## And / Or
Sometimes, you can’t neatly capture your conditions with one expression. You need to combine multiple expressions to determine what to do next.
`and`: both things must be true
`or`: Either one, or both things must be true.
```python
weather = "cold"
snowing = True
>>> if weather == "cold" and snowing == True:
... print("wear a winter jacket")
...
wear a winter jacket
>>> if weather == "raining" or snowing == True:
... print("bring a change of socks")
...
bring a change of socks
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Examples with and/or
```python
>>> x = 5
>>> if x > 0 and x < 6:
... print("x is between 0 and 6.")
...
x is between 0 and 6.
```
```python
>>> if type(x) == float or type(x) == int:
... print("x is a numeric value")
...
x is a numeric value
```
```python
>>> if x > 10 and x < 0:
... print("x could never be greater than 10 AND less than 0")
...
# Nothing prints here
```
</script>
</section>
<!-- TODO: If we have time, create an example using a numeric temperature value to decide what's cold, normal or too hot to handle -->
<section class="slide" data-markdown>
<script type="text/template">
## Exercise 3: Smarter Weather (15mins)
Instead of telling your program if it's cold, you can input the temperature and let it decide what to do.
Open the **ex3_smarter_weather.py** and use math comparison operators as well as `and`/`or` to follow the instructions.
```
# if the weather is greater than or equal to 25 degrees
if "replace this with the right condition":
print("Go to the beach!")
# the weather is less than 25 degrees AND greater than 15 degrees
elif "replace this with the right conditions":
# Still warm enough for ice cream!
else:
# Wear a sweater and dream of beaches.
```
Feel free to choose your own temperatures and messages :)
</script>
</section>
<!-- -------------------- -->
<!-- Opening a file -->
<!-- -------------------- -->
<section class="slide title full-monty">
<!-- Mandatory Monty Python reference -->
<h1 class="heading-bg"><span>And now for something completely different</span></h1>
<aside>Source: <a href="https://www.flickr.com/photos/nerdcoreblog/13995269592/">flickr.com/photos/nerdcoreblog</a></aside>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Opening a file
We typically store computer data in files. The data in some files is human-readable text (while in others, like images, it’s in a format that’s only readable by programs with specific code to interpret the data).
It’s fairly easy in Python to open a human-readable file and print the contents to the screen.
```python
>>> chapters = open('./exercises/llc-chapters.csv')
>>> print(chapters.read())
# … lots of file data …
>>> chapters.close()
# When you're done with a file it's a good idea to close it
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## One line at a time
In order to make use of the data in a file, we need to make sense of small portions at a time.
In the `llc-chapters.csv` file, each line of the file represents a Ladies Learning Code Chapter
Let’s print out a few individual lines
```python
>>> chapters = open('./exercises/llc-chapters.csv')
>>> print(chapters.readline())
City,Province,Chapter Lead(s)
>>> print(chapters.readline())
Vancouver,BC,Meghan
>>> print(chapters.readline())
Calgary,AB,Darcie
>>> chapters.close()
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Comma-Separated Values (CSV) Files
In the previous slide we printed out three lines in the file, one at a time.
```
City,Province,Chapter Lead(s)
Vancouver,BC,Meghan
Calgary,AB,Darcie
```
Notice that all three lines have exactly three values, separated by a comma (`,`)
The first line doesn’t tell us about a chapter, it has headings than describe the data in the next lines
The second and third line each represent one of the LLC chapters.
This is a specific *file format* called **CSV** (short for *comma-separated values*) that is used to represent spreadsheet-like data
This is a widely used file-format because it’s such a simple representation of rows and columns of data. It doesn’t matter whether you’re on a Mac, Windows, Linux or even on a web app like Google Docs – everyone can use this data.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Reading all the lines in a file
It’s all well and good to print out the lines in a file one-at-a-time while you’re learning, but surely, there’s a better way to read *all* the lines in a file.
In programming, when you want to perform the same action many times in a row, we use **loops**. In Python, the main kind of loop is the `for` loop.
If we want to print each of the rows in our file, we can use the following code:
```python
>>> chapters = open('./exercises/llc-chapters.csv')
>>> for line in chapters:
... print(line)
...
>>> chapters.close()
```
More info:<!-- .element: class="footnote" --> https://docs.python.org/2/tutorial/controlflow.html#for-statements
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## `for` loop behind the scenes
If you play around with the `chapters` variable in your console, you might notice that it has a `.next()` function which does the same thing as `.readline()`
`for` can work with many different collections of data, all it’s looking for is that `.next()` function.
Behind the scenes, it’s doing something roughly like
```python
line = chapters.next()
# Execute your code block
# …
# Go back to get the next value and repeat until there are no more values
```
We’ll use the `for` loop in our future exercises to work with a larger file.
</script>
</section>
<!-- <section class="slide title" data-markdown>
<script type="text/template">
# Lunch!
</script>
</section> -->
<!-- ==================== -->
<!-- Afternoon Content-->
<!-- ==================== -->
<section class="slide" data-markdown>
<script type="text/template">
## Libraries
Python has wonderful built in **libraries** which are collections of code that other people have written. Today we’ll use the **csv** library. It contains instructions that tell python how to work with CSV files.
This library will help us makes sense of the data contained in a CSV file to learn more about LLC activities in 2014.
More info:<!-- .element: class="footnote" --> https://docs.python.org/2/library/csv.html
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Using a library
So far, we've been writing all our code in the console or in one file. When you use a library, you don't want to copy all that code into your file, otherwise it would become huge!
In Python, you can **import** the functions and other structures from a **module** so you can use the code without having it in your file. Then you can use functions (and other structures) from that **module** to do your bidding!
In your console, import the `csv` library as follows.
```python
>>> import csv
```
If you then type `csv.` and wait, it will show you a list of everything in that module.
![Console with functions available in the csv module](framework/img/workshop/import-list.png)
More info:<!-- .element: class="footnote" --> https://docs.python.org/2/tutorial/modules.html
</script>
</section>
<!-- -------------------- -->
<!-- Reading a CSV -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Reading data from a CSV file
The CSV libary has a number of different tools for extracting actual data from a CSV file. Instead of a line of comma-separated values, we can access individual values easily (like the cells of a spreadsheet).
Today we’ll mostly use the `DictReader` tool because it's easier to understand the data in each row (without remembering anything about the headings)
* First we open the file
* Then, we import the `csv` library
* Then we pass the file into `DictReader` to make sense of it
```
>>> import csv
>>> chapters_file = open('llc-chapters.csv')
>>> chapters_data = csv.DictReader(chapters_file)
```
* Now, we can pull out one row at a time
```
>>> chapter = chapters_data.next()
>>> print(chapter)
{'City': 'Vancouver', 'Chapter Lead(s)': 'Meghan', 'Province': 'BC'}
>>> type(chapter)
<type 'dict'>
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Dictionaries
This time, when we printed the values from the CSV file, instead of getting comma-separated values, we got a `dict`ionary representing each row in the file.
Instead of having to remember which *column* represents city, we can access it with a keyword:
```python
>>> chapter = {'City': 'Vancouver', 'Chapter Lead(s)': 'Meghan', 'Province': 'BC'}
>>> print(chapter['City'])
Vancouver
>>> print(chapter['Province'])
BC
>>> print(chapter['Chapter Lead(s)'])
Meghan