"
1015 | ]
1016 | }
1017 | ],
1018 | "prompt_number": 11
1019 | },
1020 | {
1021 | "cell_type": "code",
1022 | "collapsed": false,
1023 | "input": [],
1024 | "language": "python",
1025 | "metadata": {},
1026 | "outputs": []
1027 | },
1028 | {
1029 | "cell_type": "code",
1030 | "collapsed": false,
1031 | "input": [],
1032 | "language": "python",
1033 | "metadata": {},
1034 | "outputs": []
1035 | }
1036 | ],
1037 | "metadata": {}
1038 | }
1039 | ]
1040 | }
--------------------------------------------------------------------------------
/part-4.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "name": "",
4 | "signature": "sha256:d806710bd05c6f416dc3842515677b9246aa116881cfd0f7dd2c6b748a3a67e5"
5 | },
6 | "nbformat": 3,
7 | "nbformat_minor": 0,
8 | "worksheets": [
9 | {
10 | "cells": [
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "Let's learn more cool Python stuff."
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "## Dictionaries\n",
23 | "\n",
24 | "Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are unique and are used for looking up values assigned to them.\n",
25 | "\n",
26 | "Let's make a Spanish to English translator. We'll ignore grammar and just translate word-by-word for now."
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "collapsed": false,
32 | "input": [
33 | "words = {\n",
34 | " 'gato': \"cat\",\n",
35 | " 'casa': \"house\",\n",
36 | " 'esta': \"is\",\n",
37 | " 'en': \"in\",\n",
38 | " 'el': \"the\",\n",
39 | " 'la': \"the\",\n",
40 | "}"
41 | ],
42 | "language": "python",
43 | "metadata": {},
44 | "outputs": []
45 | },
46 | {
47 | "cell_type": "code",
48 | "collapsed": false,
49 | "input": [
50 | "words['gato']"
51 | ],
52 | "language": "python",
53 | "metadata": {},
54 | "outputs": []
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "Similar to lists and strings, you can check whether a dictionary contains a given key."
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "collapsed": false,
66 | "input": [
67 | "'gato' in words"
68 | ],
69 | "language": "python",
70 | "metadata": {},
71 | "outputs": []
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "We can also reassign keys in our dictionary:"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "collapsed": false,
83 | "input": [
84 | "words['gato'] = \"dog\"\n",
85 | "words['gato']"
86 | ],
87 | "language": "python",
88 | "metadata": {},
89 | "outputs": []
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {},
94 | "source": [
95 | "Let's change that back so we don't confuse anyone:"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "collapsed": false,
101 | "input": [
102 | "words['gato'] = \"cat\""
103 | ],
104 | "language": "python",
105 | "metadata": {},
106 | "outputs": []
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "We can also check how many key-value pairs are in our dictionary:"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "collapsed": false,
118 | "input": [
119 | "len(words)"
120 | ],
121 | "language": "python",
122 | "metadata": {},
123 | "outputs": []
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "Strings have a split method which we can use to split strings with spaces into a list of words."
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "collapsed": false,
135 | "input": [
136 | "sentence = \"el gato esta en la casa\"\n",
137 | "sentence_words = sentence.split()\n",
138 | "sentence_words"
139 | ],
140 | "language": "python",
141 | "metadata": {},
142 | "outputs": []
143 | },
144 | {
145 | "cell_type": "markdown",
146 | "metadata": {},
147 | "source": [
148 | "Let's translate each word and save them in the list called translated_words. Let's start with an empty list and use a for loop to populate our list:"
149 | ]
150 | },
151 | {
152 | "cell_type": "code",
153 | "collapsed": false,
154 | "input": [
155 | "translated_words = []\n",
156 | "for spanish_word in sentence_words:\n",
157 | " translated_words.append(words[spanish_word])"
158 | ],
159 | "language": "python",
160 | "metadata": {},
161 | "outputs": []
162 | },
163 | {
164 | "cell_type": "code",
165 | "collapsed": false,
166 | "input": [
167 | "translated_words"
168 | ],
169 | "language": "python",
170 | "metadata": {},
171 | "outputs": []
172 | },
173 | {
174 | "cell_type": "markdown",
175 | "metadata": {},
176 | "source": [
177 | "We've almost made a translated sentence! Let's join our new list of words back together with spaces in between each word."
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "collapsed": false,
183 | "input": [
184 | "translated_sentence = \" \".join(translated_words)\n",
185 | "translated_sentence"
186 | ],
187 | "language": "python",
188 | "metadata": {},
189 | "outputs": []
190 | },
191 | {
192 | "cell_type": "markdown",
193 | "metadata": {},
194 | "source": [
195 | "Let's put this all together into a function:"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "collapsed": false,
201 | "input": [
202 | "words = {\n",
203 | " 'gato': \"cat\",\n",
204 | " 'casa': \"house\",\n",
205 | " 'esta': \"is\",\n",
206 | " 'en': \"in\",\n",
207 | " 'el': \"the\",\n",
208 | " 'la': \"the\",\n",
209 | "}\n",
210 | "\n",
211 | "def translate(sentence):\n",
212 | " spanish_words = sentence.split()\n",
213 | " english_words = []\n",
214 | " for w in spanish_words:\n",
215 | " english_words.append(words[w])\n",
216 | " return \" \".join(english_words)"
217 | ],
218 | "language": "python",
219 | "metadata": {},
220 | "outputs": []
221 | },
222 | {
223 | "cell_type": "code",
224 | "collapsed": false,
225 | "input": [
226 | "translate(\"el gato esta en la casa\")"
227 | ],
228 | "language": "python",
229 | "metadata": {},
230 | "outputs": []
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "metadata": {},
235 | "source": [
236 | "Let's add another word to our dictionary:"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "collapsed": false,
242 | "input": [
243 | "words['jugo'] = \"juice\""
244 | ],
245 | "language": "python",
246 | "metadata": {},
247 | "outputs": []
248 | },
249 | {
250 | "cell_type": "code",
251 | "collapsed": false,
252 | "input": [
253 | "translate(\"el jugo esta en la casa\")"
254 | ],
255 | "language": "python",
256 | "metadata": {},
257 | "outputs": []
258 | },
259 | {
260 | "cell_type": "markdown",
261 | "metadata": {},
262 | "source": [
263 | "That's how we say juice in Mexican Spanish but what if we're traveling to Spain? Let's remove jugo from our dictionary and add zumo:"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "collapsed": false,
269 | "input": [
270 | "del words['jugo']\n",
271 | "words['zumo'] = \"juice\""
272 | ],
273 | "language": "python",
274 | "metadata": {},
275 | "outputs": []
276 | },
277 | {
278 | "cell_type": "markdown",
279 | "metadata": {},
280 | "source": [
281 | "Let's try our Mexican Spanish sentence again. What should happen?"
282 | ]
283 | },
284 | {
285 | "cell_type": "code",
286 | "collapsed": false,
287 | "input": [
288 | "translate(\"el jugo esta en la casa\")"
289 | ],
290 | "language": "python",
291 | "metadata": {},
292 | "outputs": []
293 | },
294 | {
295 | "cell_type": "markdown",
296 | "metadata": {},
297 | "source": [
298 | "Now let's try our Spanish Spanish sentence:"
299 | ]
300 | },
301 | {
302 | "cell_type": "code",
303 | "collapsed": false,
304 | "input": [
305 | "translate(\"el zumo esta en la casa\")"
306 | ],
307 | "language": "python",
308 | "metadata": {},
309 | "outputs": []
310 | },
311 | {
312 | "cell_type": "markdown",
313 | "metadata": {},
314 | "source": [
315 | "It's also possible to loop through the keys and values of a dictionary using the items method.\n",
316 | "Let's loop through the words dictionary and see how this works:"
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "collapsed": false,
322 | "input": [
323 | "for key, value in words.items():\n",
324 | " print(key, value)"
325 | ],
326 | "language": "python",
327 | "metadata": {},
328 | "outputs": []
329 | },
330 | {
331 | "cell_type": "markdown",
332 | "metadata": {},
333 | "source": [
334 | "**Exercises:**\n",
335 | "\n",
336 | "1. Make a new py file and put that translate function in the file. Use the print function to print out some examples of using the function.\n",
337 | "\n",
338 | "2. Refactor the translate function to use a list comprehension.\n",
339 | "\n",
340 | "3. There are lots of ways to write that translate function. Just for fun, see if you can write the whole translate function in one line.\n",
341 | "\n",
342 | "**Note:** Typing out the exercises for the next section, \"Sets\", is optional. If your brain is melting, feel free to sit back, relax, and enjoy the rest of the lecture."
343 | ]
344 | },
345 | {
346 | "cell_type": "markdown",
347 | "metadata": {},
348 | "source": [
349 | "## Tuples\n",
350 | "\n",
351 | "Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n",
352 | "\n",
353 | "One real world use of tuples is to hold one particular record within a complex file. For example, a stock holding or a restaurant inventory.\n",
354 | "\n",
355 | "Tuples are comma-separated lists:"
356 | ]
357 | },
358 | {
359 | "cell_type": "code",
360 | "collapsed": false,
361 | "input": [
362 | "food = (\"cones\", 100, 3.79)"
363 | ],
364 | "language": "python",
365 | "metadata": {},
366 | "outputs": []
367 | },
368 | {
369 | "cell_type": "markdown",
370 | "metadata": {},
371 | "source": [
372 | "This is sometimes called packing a tuple."
373 | ]
374 | },
375 | {
376 | "cell_type": "code",
377 | "collapsed": false,
378 | "input": [
379 | "food"
380 | ],
381 | "language": "python",
382 | "metadata": {},
383 | "outputs": []
384 | },
385 | {
386 | "cell_type": "code",
387 | "collapsed": false,
388 | "input": [
389 | "type(food)"
390 | ],
391 | "language": "python",
392 | "metadata": {},
393 | "outputs": []
394 | },
395 | {
396 | "cell_type": "markdown",
397 | "metadata": {},
398 | "source": [
399 | "The parenthesis are often optional. They're often added for clarity. This should work too:"
400 | ]
401 | },
402 | {
403 | "cell_type": "code",
404 | "collapsed": false,
405 | "input": [
406 | "food = \"sprinkles\", 10, 4.99"
407 | ],
408 | "language": "python",
409 | "metadata": {},
410 | "outputs": []
411 | },
412 | {
413 | "cell_type": "code",
414 | "collapsed": false,
415 | "input": [
416 | "food"
417 | ],
418 | "language": "python",
419 | "metadata": {},
420 | "outputs": []
421 | },
422 | {
423 | "cell_type": "markdown",
424 | "metadata": {},
425 | "source": [
426 | "Let's see what happens when we try to change our tuple:"
427 | ]
428 | },
429 | {
430 | "cell_type": "code",
431 | "collapsed": false,
432 | "input": [
433 | "food[2] = 3"
434 | ],
435 | "language": "python",
436 | "metadata": {},
437 | "outputs": []
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {},
442 | "source": [
443 | "We can make an empty tuple with an empty set of parenthesis:"
444 | ]
445 | },
446 | {
447 | "cell_type": "code",
448 | "collapsed": false,
449 | "input": [
450 | "empty = ()"
451 | ],
452 | "language": "python",
453 | "metadata": {},
454 | "outputs": []
455 | },
456 | {
457 | "cell_type": "code",
458 | "collapsed": false,
459 | "input": [
460 | "empty"
461 | ],
462 | "language": "python",
463 | "metadata": {},
464 | "outputs": []
465 | },
466 | {
467 | "cell_type": "code",
468 | "collapsed": false,
469 | "input": [
470 | "type(empty)"
471 | ],
472 | "language": "python",
473 | "metadata": {},
474 | "outputs": []
475 | },
476 | {
477 | "cell_type": "markdown",
478 | "metadata": {},
479 | "source": [
480 | "Any guesses how we can make a single-item tuple?"
481 | ]
482 | },
483 | {
484 | "cell_type": "code",
485 | "collapsed": false,
486 | "input": [
487 | "food = (3)"
488 | ],
489 | "language": "python",
490 | "metadata": {},
491 | "outputs": []
492 | },
493 | {
494 | "cell_type": "code",
495 | "collapsed": false,
496 | "input": [
497 | "food"
498 | ],
499 | "language": "python",
500 | "metadata": {},
501 | "outputs": []
502 | },
503 | {
504 | "cell_type": "code",
505 | "collapsed": false,
506 | "input": [
507 | "type(food)"
508 | ],
509 | "language": "python",
510 | "metadata": {},
511 | "outputs": []
512 | },
513 | {
514 | "cell_type": "markdown",
515 | "metadata": {},
516 | "source": [
517 | "Python just sees this as a number with parenthesis around it. The commas are the important part. We need to add a comma after the number to make a single-item tuple:"
518 | ]
519 | },
520 | {
521 | "cell_type": "code",
522 | "collapsed": false,
523 | "input": [
524 | "food = (3,)"
525 | ],
526 | "language": "python",
527 | "metadata": {},
528 | "outputs": []
529 | },
530 | {
531 | "cell_type": "code",
532 | "collapsed": false,
533 | "input": [
534 | "food"
535 | ],
536 | "language": "python",
537 | "metadata": {},
538 | "outputs": []
539 | },
540 | {
541 | "cell_type": "code",
542 | "collapsed": false,
543 | "input": [
544 | "type(food)"
545 | ],
546 | "language": "python",
547 | "metadata": {},
548 | "outputs": []
549 | },
550 | {
551 | "cell_type": "markdown",
552 | "metadata": {},
553 | "source": [
554 | "Sometimes, unpacking a tuple is also desired."
555 | ]
556 | },
557 | {
558 | "cell_type": "code",
559 | "collapsed": false,
560 | "input": [
561 | "yummies = [(\"cones\", 100, 3.79), (\"sprinkles\", 10, 4.99), (\"hot fudge\", 8, 3.29), (\"nuts\", 6, 8.99)]"
562 | ],
563 | "language": "python",
564 | "metadata": {},
565 | "outputs": []
566 | },
567 | {
568 | "cell_type": "code",
569 | "collapsed": false,
570 | "input": [
571 | "yummies[2]"
572 | ],
573 | "language": "python",
574 | "metadata": {},
575 | "outputs": []
576 | },
577 | {
578 | "cell_type": "code",
579 | "collapsed": false,
580 | "input": [
581 | "name, amount, price = yummies[2]"
582 | ],
583 | "language": "python",
584 | "metadata": {},
585 | "outputs": []
586 | },
587 | {
588 | "cell_type": "code",
589 | "collapsed": false,
590 | "input": [
591 | "print(\"Our ice cream shop serves \" + name + \".\")"
592 | ],
593 | "language": "python",
594 | "metadata": {},
595 | "outputs": []
596 | },
597 | {
598 | "cell_type": "code",
599 | "collapsed": false,
600 | "input": [
601 | "print(\"The shop's inventory value for \" + name + \" is $\" + str(amount * price) + \".\")"
602 | ],
603 | "language": "python",
604 | "metadata": {},
605 | "outputs": []
606 | },
607 | {
608 | "cell_type": "code",
609 | "collapsed": false,
610 | "input": [
611 | "print(\"All this food talk is making me hungry for \" + yummies[3][0] + \".\")"
612 | ],
613 | "language": "python",
614 | "metadata": {},
615 | "outputs": []
616 | },
617 | {
618 | "cell_type": "markdown",
619 | "metadata": {},
620 | "source": [
621 | "Why not always use lists instead of tuples? Tuples use less storage space than a list. So if you are reading 1000s of small records from a file, a tuple can be an efficient way to go.\n",
622 | "\n",
623 | "A practical learning tip: When learning a new language, there are many new terms and concepts. Sometimes it is helpful to see a bigger picture of how everything comes together. I'm a lover of \"Table of Contents\" since I can scan the big picture and see where something like a tuple fits in. The official Python tutorial has a very good [Table of Contents]( https://docs.python.org/3.4/tutorial/index.html), and I sometimes take 5 minutes to just click and explore something new to me.\n",
624 | "\n",
625 | "Wow! We've come a long way today. "
626 | ]
627 | },
628 | {
629 | "cell_type": "markdown",
630 | "metadata": {},
631 | "source": [
632 | "## List Comprehensions\n",
633 | "\n",
634 | "Now we're going to learn about list comprehensions.\n",
635 | "\n",
636 | "A list comprehension is kind of like a reverse for-loop. It makes it easy to do operations on elements in a list and return a new list."
637 | ]
638 | },
639 | {
640 | "cell_type": "markdown",
641 | "metadata": {},
642 | "source": [
643 | "Say we have a list of numbers:"
644 | ]
645 | },
646 | {
647 | "cell_type": "code",
648 | "collapsed": false,
649 | "input": [
650 | "my_favorite_numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]"
651 | ],
652 | "language": "python",
653 | "metadata": {},
654 | "outputs": []
655 | },
656 | {
657 | "cell_type": "markdown",
658 | "metadata": {},
659 | "source": [
660 | "Now let's make a new list that contains a square of every number in our list."
661 | ]
662 | },
663 | {
664 | "cell_type": "code",
665 | "collapsed": false,
666 | "input": [
667 | "squared_numbers = []\n",
668 | "for n in my_favorite_numbers:\n",
669 | " squared_numbers.append(n * n)"
670 | ],
671 | "language": "python",
672 | "metadata": {},
673 | "outputs": []
674 | },
675 | {
676 | "cell_type": "code",
677 | "collapsed": false,
678 | "input": [
679 | "squared_numbers"
680 | ],
681 | "language": "python",
682 | "metadata": {},
683 | "outputs": []
684 | },
685 | {
686 | "cell_type": "markdown",
687 | "metadata": {},
688 | "source": [
689 | "**Stop for questions?:** Does this look familiar? Any questions about this?"
690 | ]
691 | },
692 | {
693 | "cell_type": "markdown",
694 | "metadata": {},
695 | "source": [
696 | "Now let's do the same thing by using a list comprehension instead:"
697 | ]
698 | },
699 | {
700 | "cell_type": "code",
701 | "collapsed": false,
702 | "input": [
703 | "squared_numbers = [n * n for n in my_favorite_numbers]"
704 | ],
705 | "language": "python",
706 | "metadata": {},
707 | "outputs": []
708 | },
709 | {
710 | "cell_type": "code",
711 | "collapsed": false,
712 | "input": [
713 | "squared_numbers"
714 | ],
715 | "language": "python",
716 | "metadata": {},
717 | "outputs": []
718 | },
719 | {
720 | "cell_type": "markdown",
721 | "metadata": {},
722 | "source": [
723 | "Note how our list comprehension is written within the brackets: The list we're looping over (my_favorite_numbers) is written at the end whereas the action inside of the for-loop is written first: n * n"
724 | ]
725 | },
726 | {
727 | "cell_type": "markdown",
728 | "metadata": {},
729 | "source": [
730 | "First written as a conventional 'for' loop:\n",
731 | "\n",
732 | "squared_numbers = []\n",
733 | "for n in my_favorite_numbers:\n",
734 | " squared_numbers.append(n * n)\n",
735 | "
"
736 | ]
737 | },
738 | {
739 | "cell_type": "markdown",
740 | "metadata": {},
741 | "source": [
742 | "And then written as a list comprehension:\n",
743 | "\n",
744 | "squared_numbers = [n * n for n in my_favorite_numbers]\n",
745 | "
"
746 | ]
747 | },
748 | {
749 | "cell_type": "markdown",
750 | "metadata": {},
751 | "source": [
752 | "Look for the new list being created, the iteration being done over the orignal list and the transformation being done on each element. List comprehensions are just a cleaner way of building lists from other iterables."
753 | ]
754 | },
755 | {
756 | "cell_type": "markdown",
757 | "metadata": {},
758 | "source": [
759 | "### Improving our work\n",
760 | "\n",
761 | "Let's revisit a problem we've already solved in Danny's lecture on lists:\n",
762 | "\n",
763 | "Pick every name from a list that begins with a vowel.\n",
764 | "\n",
765 | "We started with a list of names first:"
766 | ]
767 | },
768 | {
769 | "cell_type": "code",
770 | "collapsed": false,
771 | "input": [
772 | "names = [\"Danny\", \"Audrey\", \"Rise\", \"Alain\"]"
773 | ],
774 | "language": "python",
775 | "metadata": {},
776 | "outputs": []
777 | },
778 | {
779 | "cell_type": "markdown",
780 | "metadata": {},
781 | "source": [
782 | "Then we filtered names like this:"
783 | ]
784 | },
785 | {
786 | "cell_type": "code",
787 | "collapsed": false,
788 | "input": [
789 | "vowel_names = []\n",
790 | "for name in names:\n",
791 | " if name[0] in \"AEIOU\":\n",
792 | " vowel_names.append(name)"
793 | ],
794 | "language": "python",
795 | "metadata": {},
796 | "outputs": []
797 | },
798 | {
799 | "cell_type": "code",
800 | "collapsed": false,
801 | "input": [
802 | "vowel_names"
803 | ],
804 | "language": "python",
805 | "metadata": {},
806 | "outputs": []
807 | },
808 | {
809 | "cell_type": "markdown",
810 | "metadata": {},
811 | "source": [
812 | "Here's another way to grab all names starting with a vowel, using list comprehensions:"
813 | ]
814 | },
815 | {
816 | "cell_type": "code",
817 | "collapsed": false,
818 | "input": [
819 | "vowel_names = [name for name in names if name[0] in \"AEIOU\"]"
820 | ],
821 | "language": "python",
822 | "metadata": {},
823 | "outputs": []
824 | },
825 | {
826 | "cell_type": "code",
827 | "collapsed": false,
828 | "input": [
829 | "vowel_names"
830 | ],
831 | "language": "python",
832 | "metadata": {},
833 | "outputs": []
834 | },
835 |
836 | {
837 | "cell_type": "code",
838 | "collapsed": false,
839 | "input": [],
840 | "language": "python",
841 | "metadata": {},
842 | "outputs": []
843 | }
844 | ],
845 | "metadata": {}
846 | }
847 | ]
848 | }
849 |
--------------------------------------------------------------------------------
/part-5.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "kernelspec": {
4 | "codemirror_mode": {
5 | "name": "ipython",
6 | "version": 2
7 | },
8 | "display_name": "IPython (Python 2)",
9 | "language": "python",
10 | "name": "python2"
11 | },
12 | "name": "",
13 | "signature": "sha256:d3073baa9f8a050a7ae6616386382b6e189019b47ce0a3205bb089be53c07e9c"
14 | },
15 | "nbformat": 3,
16 | "nbformat_minor": 0,
17 | "worksheets": [
18 | {
19 | "cells": [
20 | {
21 | "cell_type": "heading",
22 | "level": 1,
23 | "metadata": {},
24 | "source": [
25 | "Using third-party libraries"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "Great programmers don't write all their code themselves. \n",
33 | "\n",
34 | "In fact, they write as little as possible, reusing whatever they can."
35 | ]
36 | },
37 | {
38 | "cell_type": "heading",
39 | "level": 2,
40 | "metadata": {},
41 | "source": [
42 | "Free and Open Source Software"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "There is a lot of FOSS Python code out there. It's a treasure trove for your projects:\n",
50 | "* Free to use, usually even commercially\n",
51 | "* Open source means you can study/modify the code and contribute back\n",
52 | "\n",
53 | "Usually maintained by volunteers, so:\n",
54 | "* When you use it, you're encouraged to contribute back\n",
55 | "* Be extra-friendly and nice to people who aren't getting paid to help you\n",
56 | "* If you use free Python libraries, consider creating free Python libraries too"
57 | ]
58 | },
59 | {
60 | "cell_type": "heading",
61 | "level": 2,
62 | "metadata": {},
63 | "source": [
64 | "Where to Get Free Python Libraries"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "* When you installed Python, it came with a **standard library**\n",
72 | "* The Python Package Index has 50,000 free packages for you\n",
73 | "* GitHub search\n",
74 | "* Bitbucket search"
75 | ]
76 | },
77 | {
78 | "cell_type": "heading",
79 | "level": 2,
80 | "metadata": {},
81 | "source": [
82 | "Python Package Index"
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "metadata": {},
88 | "source": [
89 | "Memorize this website URL and visit it every day:\n",
90 | "https://pypi.python.org\n",
91 | "\n",
92 | "(screenshot)\n",
93 | "\n",
94 | "Sometimes referred to as \"The Cheese Shop\" because of Monty Python\n",
95 | "\n",
96 | "(photo)"
97 | ]
98 | },
99 | {
100 | "cell_type": "heading",
101 | "level": 2,
102 | "metadata": {},
103 | "source": [
104 | "Module vs. Package vs. Library"
105 | ]
106 | },
107 | {
108 | "cell_type": "markdown",
109 | "metadata": {},
110 | "source": [
111 | "TODO"
112 | ]
113 | },
114 | {
115 | "cell_type": "heading",
116 | "level": 2,
117 | "metadata": {},
118 | "source": [
119 | "Examples of Python Packages"
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | "Here, we:\n",
127 | "* Download package X from PyPI\n",
128 | "* Import and use a function from X\n",
129 | "\n",
130 | "TODO\n",
131 | "\n",
132 | "...\n",
133 | "\n",
134 | "...\n",
135 | "\n",
136 | "(10 min using a package)"
137 | ]
138 | },
139 | {
140 | "cell_type": "markdown",
141 | "metadata": {},
142 | "source": [
143 | "What are GitHub, Gitlab, Bitbucket, Gitorious, Sourceforge, etc.?"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "TODO"
151 | ]
152 | },
153 | {
154 | "cell_type": "heading",
155 | "level": 3,
156 | "metadata": {},
157 | "source": [
158 | "Example: Package X"
159 | ]
160 | },
161 | {
162 | "cell_type": "markdown",
163 | "metadata": {},
164 | "source": [
165 | "(10 min using another package)\n",
166 | "\n",
167 | "* Show its PyPI listing\n",
168 | "* Show its GitHub repo\n",
169 | "* Click through and show its files and docs"
170 | ]
171 | },
172 | {
173 | "cell_type": "heading",
174 | "level": 3,
175 | "metadata": {},
176 | "source": [
177 | "What is Version Control?"
178 | ]
179 | },
180 | {
181 | "cell_type": "markdown",
182 | "metadata": {},
183 | "source": [
184 | "TODO"
185 | ]
186 | },
187 | {
188 | "cell_type": "heading",
189 | "level": 3,
190 | "metadata": {},
191 | "source": [
192 | "DVCS and Collaboration"
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "TODO"
200 | ]
201 | },
202 | {
203 | "cell_type": "heading",
204 | "level": 2,
205 | "metadata": {},
206 | "source": [
207 | "What's in a Python Package?"
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "TODO"
215 | ]
216 | },
217 | {
218 | "cell_type": "heading",
219 | "level": 2,
220 | "metadata": {},
221 | "source": [
222 | "Using Third-Party Libraries in Projects"
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {},
228 | "source": [
229 | "Keeping track of requirements\n",
230 | "\n",
231 | "Avoiding dependency conflicts"
232 | ]
233 | },
234 | {
235 | "cell_type": "markdown",
236 | "metadata": {},
237 | "source": []
238 | }
239 | ],
240 | "metadata": {}
241 | }
242 | ]
243 | }
--------------------------------------------------------------------------------
/part3/ex01/birthday.py:
--------------------------------------------------------------------------------
1 | # Script that prints greeting directly
2 |
3 | print("Happy Birthday, dear Carol!")
4 |
--------------------------------------------------------------------------------
/part3/ex02/birthday.py:
--------------------------------------------------------------------------------
1 | # Script that prints greeting by calling function
2 |
3 | def happy_birthday_carol():
4 | print("Happy Birthday, dear Carol!")
5 |
6 | happy_birthday_carol()
7 |
--------------------------------------------------------------------------------
/part3/ex03/birthday.py:
--------------------------------------------------------------------------------
1 | # Script that prints 2 greetings via 2 function calls
2 |
3 | def happy_birthday_carol():
4 | print("Happy Birthday, dear Carol!")
5 |
6 | def happy_birthday_rise():
7 | print("Happy Birthday, dear Rise!")
8 |
9 | happy_birthday_carol()
10 | happy_birthday_rise()
11 |
--------------------------------------------------------------------------------
/part3/ex04/birthday.py:
--------------------------------------------------------------------------------
1 | def happy_birthday(name):
2 | print("Happy Birthday, dear " + name + "!")
3 |
4 | happy_birthday("Trey")
5 |
--------------------------------------------------------------------------------
/part3/ex05-exercise/answer01a.py:
--------------------------------------------------------------------------------
1 | # Script that wishes happy birthday to Wolfe+585, Senior
2 | # http://en.wikipedia.org/wiki/Wolfe+585,_Senior
3 |
4 | def happy_birthday(name):
5 | print("Happy Birthday, dear " + name + "!")
6 |
7 | # This guy is a real-life test case for name fields
8 |
9 | happy_birthday("Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Zeus Wolfeschlegelsteinhausenbergerdorffvoralternwarengewissenhaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifendurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenwanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortplanzenundsicherfreuenanlebenslanglichfreudeundruhemitnichteinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischensternartigraum, Senior")
10 |
--------------------------------------------------------------------------------
/part3/ex05-exercise/answer01b.py:
--------------------------------------------------------------------------------
1 | # Script that wishes happy birthday to the Gettysburg Address
2 |
3 | def happy_birthday(name):
4 | print("Happy Birthday, dear " + name + "!")
5 |
6 | happy_birthday("""Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
7 |
8 | Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.
9 |
10 | But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.""")
11 |
12 |
--------------------------------------------------------------------------------
/part3/ex05-exercise/answer01c.py:
--------------------------------------------------------------------------------
1 | # Script that wishes happy birthday to people who have number names
2 |
3 | def happy_birthday(name):
4 | print("Happy Birthday, dear " + name + "!")
5 |
6 | # Try one of these
7 |
8 | happy_birthday(100)
9 | happy_birthday(3.1415927)
10 |
--------------------------------------------------------------------------------
/part3/ex05-exercise/answer02.py:
--------------------------------------------------------------------------------
1 | # Script that prints greeting 4 times via refactored function
2 |
3 | def happy_birthday(name):
4 | print("Happy Birthday, dear " + name + "!")
5 |
6 | names = [5, "Carol", "Rise", "Trey", "Alain"]
7 | for name in names:
8 | happy_birthday(name)
9 |
--------------------------------------------------------------------------------
/part3/ex05/birthday.py:
--------------------------------------------------------------------------------
1 | # Script that prints greeting 4 times via refactored function
2 |
3 | def happy_birthday(name):
4 | print("Happy Birthday, dear " + name + "!")
5 |
6 | names = ["Carol", "Rise", "Trey", "Alain"]
7 | for name in names:
8 | happy_birthday(name)
--------------------------------------------------------------------------------
/part3/ex06/hi.py:
--------------------------------------------------------------------------------
1 | def hi(first, last):
2 | print("Hi {} {}".format(first, last))
3 |
4 | hi("Henry", 8)
--------------------------------------------------------------------------------
/part3/ex07/hi.py:
--------------------------------------------------------------------------------
1 | def hi(first, last):
2 | print("Hi {} {}".format(first, last))
3 |
4 | hi("Henry", 8)
5 |
--------------------------------------------------------------------------------
/part3/ex08/factors.py:
--------------------------------------------------------------------------------
1 | def print_factors(age):
2 | for i in range(1, age + 1):
3 | if age % i == 0:
4 | print(i)
5 |
6 | print_factors(32)
--------------------------------------------------------------------------------
/part3/ex09/cupcakes.py:
--------------------------------------------------------------------------------
1 | def cupcake_tally(guests):
2 | """ Given number of party guests,
3 | returns how many cupcakes are needed. """
4 | return 2 * guests + 13
5 |
6 | cupcakes = cupcake_tally(30) + cupcake_tally(1)
7 | print("You need {} cupcakes".format(cupcakes))
--------------------------------------------------------------------------------
/part3/ex10/cupcakes.py:
--------------------------------------------------------------------------------
1 | def cupcake_tally(guests):
2 | """ Given number of party guests,
3 | returns how many cupcakes are needed. """
4 | return 2 * guests + 13
5 |
--------------------------------------------------------------------------------
/part3/ex10/main.py:
--------------------------------------------------------------------------------
1 | from cupcakes import cupcake_tally
2 |
3 | tally = cupcake_tally(30) + cupcake_tally(1)
4 | print("You need {} cupcakes".format(cupcakes))
--------------------------------------------------------------------------------
/slides/20-things-part-a.key:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/20-things-part-a.key
--------------------------------------------------------------------------------
/slides/20-things-part-a.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/20-things-part-a.pdf
--------------------------------------------------------------------------------
/slides/20-things-part-b.key:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/slides/20-things-part-b.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/20-things-part-b.pdf
--------------------------------------------------------------------------------
/slides/conclusion.key:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/conclusion.key
--------------------------------------------------------------------------------
/slides/conclusion.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/conclusion.pdf
--------------------------------------------------------------------------------
/slides/part-3.key:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/part-3.key
--------------------------------------------------------------------------------
/slides/part-3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PythonWorkshop/intro-to-python/9f8ad86e42e40d059877fd234fb160602e8907ac/slides/part-3.pdf
--------------------------------------------------------------------------------
/watermark.txt:
--------------------------------------------------------------------------------
1 | Watermark is a project by Sebastian Rasbt (GitHub @rasbt).
2 | It's a nifty "IPython magic extension for printing date and time stamps, version numbers, and hardware information".
3 |
4 | To check if our workshop's IPython notebook is running under 3.4.1, the following 3 line snippet can be pasted into a cell:
5 | %install_ext https://raw.githubusercontent.com/rasbt/watermark/master/watermark.py
6 | %load_ext watermark
7 | %watermark
8 |
--------------------------------------------------------------------------------