-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
863 lines (629 loc) · 16.4 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Python</title>
<meta name="description" content="Intro to Python Workshop for Iowa Tech Chicks">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/white.css" id="theme">
<!-- ITC theme -->
<link rel="stylesheet" href="css/itc.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
</section>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Intro to Python Workshop</h1>
<p>
<small>
Presented by: <br><br>
Jennifer Reiber Kyle / <a href="http://twitter.com/jreiberkyl" target="blank">@jreiberkyle</a>
<br>and <br>
<a href="http://iowatechchicks.com" target="blank">Iowa Tech Chicks</a> / <a href="http://twitter.com/iowatechchicks" target="blank">@iowatechchicks</a>
</small>
</p>
</section>
<!-- Slides are separated by newline + three dashes + newline, vertical slides identical but two dashes -->
<section data-markdown data-separator="^\n---\n$" data-separator-vertical="^\n--\n$">
<script type="text/template">
## Slides Source
Hosted on Github:
https://github.com/jreiberkyle/intro-to-python
---
## About me
Jennifer Reiber Kyle
- Imagery Scientist at [Planet Labs](https://www.planet.com/)
- Analyze and process [satellite imagery](https://www.planet.com/gallery/)
- Lead dev on 2 Planet Labs Open Source Python libraries
- [color balancing](https://github.com/planetlabs/color_balance)
- [radiometric normalization](https://github.com/planetlabs/radiometric_normalization)
- Over 3 years experience developing in Python
---
## What about you?
---
## What you will achieve
Learn the basics of programming
Learn the basics of Python
Develop a hangman game in Python
---
## A Tale of 2 Parts
#### Part 1: The Basics
- Python Language
- Hello, World!
- Programming Basics
#### Part 2: Advanced Control
---
## Python Language
###What Is Python
- Python is an interpreted programming language
- What is a programming language?
- Why do we need one?
---
### Your computer speaks another language
![binary code](./images/binary-503577_640.jpg)
---
## Why python is great
- **Easy to use:** It is closer to human language than to machine language.
- **Powerful:** It’s used in academic research, Google, NASA, and Planet Labs
- **Runs Everywhere:** It’s platform independent. You can write your
program on a Mac and run it on Windows.
- **Free and Open Source.**
- **Great Community.**
---
## Hello, World!
```python
print "Hello, world!"
```
- Python Shell
- Text Editor / Python Interpreter
- Online Interpreter
---
## Python Shell
- Open terminal (cmd-Space)
- Type `python`
```
Python 2.7.8 (default, Oct 15 2014, 22:04:42)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
```
- `$` means you are in the terminal (not python)
- `>>>` means you are in python
- To leave intepreter, type `exit()`
---
## Text Editor / Python Interpreter
**Text Editor**: A program that opens text files and allows the user to edit and save them.
(Different than a word processor)
- **Built in**: vi
- **3rd Party:** MacVim, Atom.io, Sublime Text, Notepad++, Brackets ...
- **My Favorite:** Sublime (lots of plugins, fuzzy search)
---
## vi
-
- Open terminal (cmd-Space)
- Type `vi helloworld.py`
- Enter insert mode (i)
- Add command
- Exit insert mode (ESC)
- Save (:wq)
- Type `python helloworld.py`
---
## Online Interpreter
- [repl.it](http://repl.it/languages/Python): (http://repl.it/languages/Python)
- right pane: Interactive
- left pane: Text editor (run using arrow)
---
# Programming Basics
- Math
- Types
- Built-in Functions
- Variables
- I/O
- Conditional Logic
---
Learning to Code is as Simple as ABC
#Always
#Be
#Coding
---
## Math
Python is good at math
```
>>> 1 + 1
2
>>> 2 - 3
-1
>>> (345 + 3) * 9
3132
```
---
## Math Operators
- ``+`` Addition
- ``–`` Subtraction
- ``*`` Multiplication
- ``/`` Division
---
##Exercise
Try some math
- Does Python follow order of operation rules?
- Did division return the whole result?
---
We can use more than just numbers
```
>>> "Buenos" + " dias"
'Buenos dias'
>>> 'or I can use single quotes'
'or I can use single quotes'
```
---
But what if we combine text and numbers?
```
>>> "I'll see you at " + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str'
and 'int' objects
```
---
## Types
- Python is extremely picky and needs to classify everything before it
knows what to do with it
- `“I’ll see you at “` is a String
- `2` is an Integer
- Use the `type()` function to tell what type an object is
---
## Exercise
What is the result of the following:
- `type("Iowa Tech Chicks")`
- `type(6)`
- `type(6.1)`
- `type(6.0)`
---
## Type casting
```
>>> "I'll see you at " + str(2)
"I'll see you at 2"
>>> 1 + int("2")
3
```
---
## Built-in Functions
- What if you want to do something more complex than addition,
subtraction, etc?
- Python gives us functions to do more complex operations
- Functions act on specific types
---
## Number Functions
- `abs(n)`
- `pow(n, e)`
```
>>> abs(-7)
>>> pow(2, 8)
```
Hint: to figure out what a function does use the help function (e.g. `help(abs)`)
---
## String Methods
Functions specific to the string object
The string is the first argument
- `.capitalize()`
- `.replace()`
- `.lower()`
```
>>> "iowa".capitalize()
>>> "Iowa Tech Chicks".replace("Chicks","Peeps")
>>> "MOAR PYTHON!!!".lower()
```
---
## Exercise
Try the following String methods
- `title`
- `strip`
- `index`
- `split`
Hint: to figure out what amethod does use the help function (e.g. `help("f".strip`)
---
But functions only work on certain types
```
>>> pow("elephant", "brain")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(
s) for ** or pow(): 'str' and 'str'
```
---
## Variables
Keeping your code **DRY**
**D**on’t **R**epeat **Y**ourself
```
>>> password =
"SuperSecure"
>>> password
'SuperSecure'
>>> num1 = 4
>>> num1
4
>>> num2 = 1234.5
>>> num2
1234.5
```
---
## Why variables are useful
- Variable means “liable to change”
- Variables change throughout the lifetime of our program
- Without variables our programs wouldn’t be able to change
- Setting a fixed value in a program is called ‘hard coding’
---
## Exercise
Let’s throw a party
```
>>> party_budget = 1000
>>> angel_round = 100000
>>> party_budget + angel_round
101000
```
What is your party budget now?
---
```
>>> party_budget
1000
```
We have to tell the party_budget it has a new value
```
>>> party_budget = party_budget + angel_round
>>> party_budget
101000
```
---
The guestlist
```
>>> guestlist = "Peter June Shreek"
>>> guestlist = guestlist + " Roberto"
>>> guestlist
'Peter June Shreek Roberto'
```
---
Anything you can do to a ‘literal’ you can do
to a variable
```
>>> startup = "my elephant brain"
>>> startup.title()
'My Elephant Brain'
>>> revenue = -429376
>>> abs(revenue)
429376
>>> revenue + 1000000
570624
```
---
But did the variable change?
```
>>> startup
'my elephant brain'
```
The function returned a new value, it did not change the original value.
---
## Exercise
- Try using the math operators we saw earlier on variables
- `abs(n)`
- `pow(n, x)`
---
## Comments
- Python ignores anything after the # character
```
# This is a comment
print 'Hello World' # so is this
# print("this won't be printed")
```
---
## Timeout for Errors
- There are different kinds of errors that can occur
- A runtime error results in an Exception
- An Exception gives us some information about the nature of the error and how to correct it
---
## Errors - continued ...
**SyntaxError** - Does not conform to the Python rules (syntax)
```python
1 + lego(prog(
```
**NameError** - Using a name that has not been defined yet
```python
a = 1
print b
```
**TypeError** - Using an object in a way that its type does not support
```python
'hat' / 'dog'
```
---
## I/O - Asking for Input
`raw_input()` (`input()` in Python3)
```
#raw input prompts the user
raw_input("Please enter your name:\n")
#store the result in a variable
my_name = raw_input("Please enter your name:\n")
#store the result in a variable
my_age = int(raw_input("How old are you?\n"))
```
---
## I/O - Printing
```
>>> party_budget = 1500
>>> print party_budget
```
---
## Special Characters
- `\n`
- `\`
- `\’`
- `\\`
- `\t`
```
>>> print "215 Spadina \nToronto \nOntario"
215 Spadina
Toronto
Ontario
```
---
## String formatting
String Format method
```
>>> utensil =
"spoon"
>>> "My {} is too big".format(utensil)
'My spoon is too big'
>>> "The {0} came before the {1}".format('chicken', 'egg')
'The chicken came before the egg'
>>> "The {1} came before the {0}".format('chicken', 'egg')
'The egg came before the chicken'
>>> template = "The Jaguar F-Type has {}hp"
>>> template.format(488)
'The Jaguar F-Type has 488hp'
```
---
## Exercises
- Use multiple placeholders but only one argument
- Print centered text
- ' centered '
- Print centered text and pad with plus signs
- '+++++++++++centered+++++++++++'
- Look at http://docs.python.org/2/library/string.html#formatstrings
for help
---
## Recap
- Programs
- Programming languages are intermediates between computer and
human language
- Programs are sets of instructions
- Languages have ‘types’: string, integer, float, ...
- Built-in Functions
- add behaviour
- most work on only one type
---
## Recap
- Variables
- save us from repeating ourselves
- don’t change until you tell them to
- Anything you can do to a literal you can do to a variable
- I/O
- input: use raw_input() / input()
- output: use print() and format()
---
## Exercise
### A Ticket App
Peter’s Python Prognostications
- Here’s how it works
1. We start with 8 tickets
2. We ask the user how many they want
3. We print out that number
4. We show the user how many tickets are left
---
- Start with 8 tickets
- Ask the user how many they want
- Print out that number
- Show the user how many tickets are left
```
num_tickets = 8
tickets_requested = # ask the user how many tickets she wants
print "You've asked for {} ticket(s)".format(tickets_requested)
# deduct the tickets sold from available tickets
print #the number of remaining tickets
```
###SAVE FOR LATER
---
```
num_tickets = 8
tickets_requested = raw_input("How many tickets do you want?\n")
tickets_requested = int(tickets_requested)
print "You've asked for {} ticket(s)".format(tickets_requested)
# deduct the tickets sold from available tickets
num_tickets = num_tickets - tickets_requested
#the number of remaining tickets
print "There are {} ticket(s) left".format(num_tickets)
```
---
###Issues you may have had
- raw_input always returns a string
- You’ll need to cast it to an int
- Did you forget to close a quotation mark?
- It happens to the best of us
- num_tickets wasn’t updated
- You have to explicitly update the number
- This is called tracking ‘state’
---
## Conditional Logic
- Programs don’t run line by line from start to end
- They jump around according to rules
- One way to specify those rules is to use conditions
---
## Conditional Logic
- We frequently want to test if two values are equal
- For equality we use the double equal sign
- ``1==1``
- We can test if two things aren’t equal with ``!=``
- ``1!=2``
- There are also many of the math operators we know
- Greater than: ``>``
- Greater than or equal to: ``>=``
- Less than: ``<``
- Less than or equal to: ``<=``
---
## Exercise
- In the Python shell use all the operators on the previous slide
---
## Boolean types
- Either true or false
- Useful when making a decision
---
if statement
```
if 1==1:
print "I am the walrus!"
```
---
else Statement
```
if 0:
print "0 is always false"
else:
print "So the else clause is executed"
```
---
elif statement
```
today = "Saturday"
if today == "Friday":
print "Everybody’s working for the weekend!"
elif today == "Saturday":
print "S-A-T-U-R-D-A-Y Night!"
elif today == "Sunday":
print "Pancakes."
else:
print "Early to bed and early to rise."
```
---
## Exercise
### A Ticketing App (cont.)
Stop the user from buying tickets if they ask for more than are
available
---
```
num_tickets = 8
tickets_requested = raw_input("How many tickets do you want?\n")
tickets_requested = int(tickets_requested)
print "You've asked for {} ticket(s)".format(tickets_requested)
# deduct the tickets sold from available tickets
if tickets_requested > num_tickets:
print "We're sorry. We don't have that many tickets"
else:
num_tickets = num_tickets - tickets_requested
print "Purchase successful"
#the number of remaining tickets
print "There are {} ticket(s) left".format(num_tickets)
```
---
## Exercise
###Color
Write a program that asks you what your favourite color is and tells
you something else that is the same color
```
What is your favourite color?
Green
I like turtles
```
---
## Color
```
fav = raw_input('What is your favourite color?\n')
fav_lower = fav.lower()
if fav_lower == 'green':
print 'I like turtles'
elif fav_lower == 'blue':
print 'Like the sky'
elif fav_lower == 'purple':
print 'My favourite ice cream when I was young was called Purple People Eater'
else:
print "{} is my favourite too.".format(fav)
```
---
## Exercise
###And/Or
- Write a program that prints a string if the input is less than 10 or
greater than 20
- Do the same thing for greater than 10 and less than 20
---
###and/or
```
input = raw_input("Give me a number: ")
input = int(input)
if input < 10 or input > 20:
print 'That\'s a good number'
if input > 10 and input < 20:
print "You've found the sweet spot"
```
---
## What we’ve covered
- Programs
- Basic math with Python
- Types
- Functions
- Variables
- I/O (input and printing)
- Conditional logic
</script>
</section>
<section>
<h1>Thank You!</h1>
<br>
<p>Support Iowa Tech Chicks at <a href="http://www.iowatechchicks.com/support/">iowatechchicks.com/support</a></p>
<br>
<p>Connect with me on Twitter (@jreiberkyle) or Github (github/jreiberkyle)</p>
<br>
<p>Stick around for <a href="part2.html">Part 2: Advanced Control</a></p>
</section>
<footer>
<br>Jennifer Reiber Kyle
</footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return true; }, callback: function() { hljs.initHighlightingOnLoad(); } },
// { src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>