forked from knowcliu/llc-intro-to-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.html
1350 lines (1014 loc) · 47.1 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
https://github.com/LeaVerou/csss -->
<link rel="stylesheet" href="framework/css/slideshow.css" data-noprefix>
<!-- Theme-specific styles -->
<!-- <link href='http://fonts.googleapis.com/css?family=Montserrat:400,700%7CQuicksand%7CPacifico%7COpen+Sans:400italic,400,300' rel='stylesheet' type='text/css' data-noprefix> -->
<link rel="stylesheet" href="framework/css/font-awesome.min.css" data-noprefix>
<link rel="stylesheet" href="framework/css/fonts.css" data-noprefix>
<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 class="en" data-duration="360">
<!-- Welcome slide -->
<header class="slide">
<img class="logo" src="framework/img/logo-llc-white.png" alt="Ladies Learning Code logo">
<h1>Welcome to Ladies Learning Code!</h1>
<div class="instructions">
<!-- Add wifi info here -->
<!-- <h2>Get Connected</h2>
<p>Network: <br>
Password: </p> -->
<h2>Download</h2>
<p>Learner files: <a href="https://github.com/ladieslearningcode/llc-intro-to-python/archive/master.zip">http://bitly.com/llc-python</a> (zip file)</p>
<ul>
<li>unzip the learner file download (<em>extract all</em> if you’re on a PC)</li>
<li>open <strong>slides.html</strong> in your browser to view the presentation</li>
</ul>
<p>Install Python 2.7.x</p>
<ul>
<li>Windows - <a href="https://www.python.org/downloads/windows/">https://www.python.org/downloads/windows/</a></li>
<li>Mac - pre-installed. Go to <strong>Applications > Utilities > Terminal</strong><br>
In the terminal window type "python" to confirm installation.</li>
</ul>
<p>PyCharm Educational Edition<br>
<a href="https://www.jetbrains.com/pycharm-educational">https://www.jetbrains.com/pycharm-educational</a>
</p>
</div>
<div class="sponsor">
<p>In partnership with
<img src="framework/img/telus-logo-white.svg" alt="Telus logo"></p>
</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>Data Insights with Python for Beginners</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 class="sponsor">
<p>In partnership with</p>
<img src="framework/img/telus-logo-white.svg" alt="Telus logo">
</div>
</div>
</section>
<section class="slide project" data-markdown>
<script type="text/template">
# Today’s project
Imagine you’re organizing **National Learn to Code Day 2016**.
![](framework/img/workshop/learn-to-code-day.jpg)
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 interesting stats to put in a press release.
Today, you’ll learn to 'think' like a computer as you gather insights into who attended Ladies Learning Code workshops in 2015.
</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 & counting patterns
* Final Project
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## What is programming?
* Programming is basically giving instructions to your computer in a form that it can understand
* Computers need *very specific* instructions
* Programming languages have syntax, just like human languages have grammar rules
<br>
### Why Python Is Awesome
* Free!
* Open source
* Write once, run anywhere
* Great community
* The syntax looks similar to 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">
![Kind of a big deal](framework/img/workshop/kind-of-a-big-deal.jpg) <!-- .element: style="width: 40%" -->
</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. [Learner files from LLC (containing the exercise and reference files)](https://github.com/ladieslearningcode/llc-intro-to-python/archive/master.zip).
* make sure the **llc-intro-to-python-master.zip** file has been unzipped (and extracted for PC users) so you can access the contents of the folder
</section>
<section class="slide centered" data-markdown>
<script type="text/template">
## PyCharm IDE <br>(Integrated Development Environments)
Use IDEs to write code faster with fewer mistakes.
<br><br>
![That is awesome!](framework/img/workshop/awesome_i_hear_that_psych.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**.
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 Project Tool Window. Select the arrows to open the folders within. Today, you will be working from the **exercises** folder (**exercises > fr** for French).
The **reference** folder contains the answer keys.
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 is 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. Type code (instructions) after the green `>>>` symbol. This symbol is used to represent a space to enter your input but you don’t have to actually type it.
![Python Console in PyCharm](./framework/img/workshop/open-console2.png)
**Pro tip!** You can also open and close the console by selecting the icon in the lower left 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
The previous example used the *integer* number **type** but there are many more.
* Integer (`int`, whole numbers)
* Float (`float`, numbers containing decimals)
* String (`str`, words, sentences, literal text)
* Boolean (`True`, `False`)
* [and many more!](http://www.diveintopython3.net/native-datatypes.html)
To display the type, use `type()` and a data value inside the parentheses.
```
>>> type("hello")
<type 'str'>
>>> type(42)
<type 'int'>
>>> type(3.14159)
<type 'float'>
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Functions
* Functions are bundles of instructions that can be re-used
* Reusing functions helps to reduce redundancy
* Function can take **arguments** and can **return** values
e.g. the `add` function takes `a` and `b` as arguments and will **return** the result
```python
>>> def add(a, b):
... return a + b
...
>>> add(1, 2)
3
>>> add(3, 4)
7
```
It is possible to create your own but today's exercises will use functions provided by Python and its libraries.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Printing
The `print()` **function** outputs values to the screen.
Note the syntax in the examples. Quotes should be included when using a *string* data type.
```
>>> print("Hello World!")
Hello World!
```
```
>>> print(2 + 2)
4
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Concatenation
**Concatenation** is used to "add" strings together by joining multiple strings into one.
```
>>> print("Hello " + "World!")
Hello World!
```
<br>
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 the data **type** 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'
```
<br>
The first `2` is an integer and the second `"2"` is a **string** type.
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
In Python, you can *change the type*.
In this example, the `int()` function can be used to convert the `"2"` string into a number.
```
>>> print(2 + int("2"))
4
```
<br>
Remember the `1/2 = 0` example? When Python divides two integers, it rounds the result down to the nearest integer (in this case 0).
Integer **types** are whole numbers (no decimals), so to be more precise, `float()` can be used to include 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. This example below will return an error. Why?
```
>>> print(99 + " bottles of beer on the wall")
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
```
<br>
Use the `str()` function to convert a number to a string.
```
>>> print(str(99) + " bottles of beer on the wall")
```
<br>
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
* Used to store a value to use whenever you need it
* Variables store data in the computer’s memory and a **variable name** is used 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 the initial declaration 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?<!-- .element: class="delayed" -->
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 list-font-default" data-markdown>
<script type="text/template">
## What's in a name?
Here are some tips & best practices for creating variable names:
* variable names can only use letters, numbers and the underscore (`_`)
* no spaces!
* separate words with the underscore `_` symbol
* e.g. `ladies_learning_code`
* they cannot begin with a number
* `1day` gives you an error, `day1` will work.
* use descriptive names
* e.g. `first_name` is more descriptive than `fn`
</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 can also use comments to organize your code or to "hide" chunks of code you don't want to delete but are not ready to use yet. Feel free to add your own throughout the exercises!
Note the syntax below:
```
# 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
Instead of just a 'Hello World' message on the screen, take it one step further and have Python say hello to anyone!
Try using the `raw_input()` function. This will get the data inputed by a user and **returns** that value back to you.
1. Give `raw_input()` a message to let the user know what information is being requested using this syntax (note the quotes):
```python
>>> raw_input("What's your name?")
```
1. The user types a response and hits the **enter** key
1. Whatever the user types is **returned** to you.
Try it in your console. It will look something like this:
![](framework/img/workshop/raw_input.gif)
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Class Exercise 1: Hello World (part 2)
Using the console is great for testing smaller chunks of code but there is a better way. Python can run code from a file with a `.py` file **extension**. Let’s create a Python file!
1. Right-click (or two-finger tap) 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. Add the below program/code snippet to the file. Try to practice typing it out to get used to the syntax. Use this example as a reference rather than copy-paste.
Reminder, anything after the `#` symbol is just a comment.
```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)
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Class Exercise 1: Hello World (part 3)
Code in a **.py** file is just text until you **run** it through the Python program.
1. To run the code, select the green arrow symbol. PyCharm automatically saves your file before you run it.
![](framework/img/workshop/pycharm-run.png)
You can also find it in the menu under **View > Tool Windows > Run**
1. The code will run in your console window. Type your name and press the **Enter** key to see your response repeated back at you.
![Demo of hello_world example](./framework/img/workshop/ex1-hello-world.png)
<br>
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 decide what to do instead? `if` statements can be used to execute different sets of instructions based on specific conditions.
Each `if` statement contains a test. The indented block is the code that will run if the test is `True` 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`? Use 'Boolean' statements to test against the values.
Reminder, one equals symbol only *assigns* a value to the variable.
```python
>>> weather = "raining"
```
The double equals symbol (`==`) will evaluate if the value on the right matches the *assigned* value and can be used to test the values.
```
>>> weather == "cold"
False
>>> weather == "raining"
True
```
</script>
</section>
<!-- -------------------- -->
<!-- Exercise 2: Weather -->
<!-- -------------------- -->
<section class="slide" data-markdown>
<script type="text/template">
## Exercise 2: Weather (10 mins)
Create a new file named `ex2_weather_advice.py`.
Use the code snippet below as a reference but try to type it out rather than copy-paste to get used to the syntax.
Note the whitespace. Python uses indentation to signify which block of code belongs inside the `if` or `else`.
```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 :)")
```
**Bonus: ** Try running your file a few times to get different advice and/or update the conditions or add more conditions.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Indentation
When typing `if` or `else` in the console (instead of a .py file), you'll see something like 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 with Math Comparison Operators
Math operators can also be used to test numeric values. Here are a few we will use today.
`>` 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")
...
x 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 may 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
In math notation you can write `0 < x < 6`, but in Python, you should split these into separate comparisons.
```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>
<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 existing **ex3_smarter_weather.py** file
* use math comparison operators, `and`/`or` to complete the exercise
* follow the instructions in the comments
* feel free to choose your own temperatures and messages :)
```
# 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.
```
</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
Computer data is often stored in files such **.csv** (comma-separated values) files.
These file types are used to represent spreadsheet-like data.
Let's look at the example below to see how to open these files in Python and print the contents to the screen.
Back in the console, try adding the below commands.
When you open a file using the "with" syntax, that creates a new block and the file will be closed automatically for you as soon as you exit the block. In the terminal, you press enter twice after the last line in the block to exit the block
```python
>>> with open('./exercises/llc-chapters.csv') as chapters:
>>> print(chapters.read())
>>>
# ... lots of file data ...
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Comma-Separated Values (CSV) Files
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 try printing out a few individual lines using the example below. Remember, only add the code after the `>>>` symbol.
```python
>>> with open('./exercises/llc-chapters.csv') as chapters:
>>> print(chapters.readline())
City,Province,Chapter Lead(s)
>>> print(chapters.readline())
Vancouver,BC,Meghan
>>> print(chapters.readline())
Calgary,AB,Darcie
```
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## One line at a time
In the previous slide we printed out three lines in the file, one at a time.
Notice that all three lines have exactly three values, separated by a comma.
```
City,Province,Chapter Lead(s)
Vancouver,BC,Meghan
Calgary,AB,Darcie
```
* the first line shows the headings that describe the data in the next lines
* the second and third line each represent one of the LLC chapter cities & the name of the chapter lead
That's because it's pulling the data from the rows & columns of the CSV file.
</script>
</section>
<section class="slide" data-markdown>
<script type="text/template">
## Loops: Reading all the lines in a file
We've just seen how to print out one line of a time but what if you need to read all the lines in the file?
In programming, to perform the same action many times in a row, **loops** are used.
In Python, try using the `for` loop to print each of the rows in the file.
Here's some sample code:
```python
>>> with open('./exercises/llc-chapters.csv') as chapters:
>>> for line in chapters:
... print(line)
...
```
**Pro tip!** After adding `print(line)` & enter, press enter again to execute the loop.
More info: 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 many useful built in **libraries** — collections of pre-written code.
Today we’ll use the **csv** library to help us make more sense of the data contained in a CSV file and learn more about the LLC activities in 2015.
More info: https://docs.python.org/2/library/csv.html
![](framework/img/workshop/genius-glee.gif)
</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!
Instead, **import** the functions and other structures from a **module** to use the code without actually adding all of the code into your file.
In your console, import the `csv` library as follows:
```python
>>> import csv
```
Then type `csv.` and wait. It will show a list of everything in that module.
![Console with functions available in the csv module](framework/img/workshop/import-list.png)
More info: 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 library has a number of different tools for extracting data from a CSV file. Instead of a line of comma-separated values like in the previous example, we can access individual values (like the cells of a spreadsheet).