|
| 1 | + <!-- Let's develop it: 15 minutes --> |
| 2 | + <section> |
| 3 | + <h3>Let's Develop It</h3> |
| 4 | + <p>Write a program that opens a text file and does some processing.</p> |
| 5 | + <ul> |
| 6 | + <li>The program should take a word as input and determine if the word appears in the file</li> |
| 7 | + <li>The program should use at least one function to do its work and you should be able to import this function in a Python shell and call it with a word and filename</li> |
| 8 | + <li>Use the functions from <a href="http://calebsmith.github.io/gdi-intro-python/examples/helpers.py">helpers.py</a> to help with reading in the lines and/or words of the file</li> |
| 9 | + <li>Download a book in plain text from <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenburg</a> and put it into the same directory as your python file.</li> |
| 10 | + </ul> |
| 11 | + <p>The next slide has some code and other resources that should help you get started</p> |
| 12 | + </section> |
| 13 | + |
| 14 | + <section> |
| 15 | + <h3>Let's Develop It- Example Code</h3> |
| 16 | + <pre><code contenteditable class="python"> |
| 17 | +from helpers import generate_cleaned_lines |
| 18 | + |
| 19 | +def is_word_in_file(word, filename): |
| 20 | + for line in generate_cleaned_lines(filename): |
| 21 | + # line will be a string of each line of the file in order |
| 22 | + # Your code goes here. |
| 23 | + # Your code should do something with the word and line variables and assign the value to a variable for returning |
| 24 | + |
| 25 | +input_word = raw_input("Enter a word to search for:") |
| 26 | +answer = is_word_in_file(input_word, 'pride.txt') |
| 27 | +# Display the answer in some meaningful way |
| 28 | + </code></pre> |
| 29 | + <p>I have used <a href="http://calebsmith.github.io/gdi-intro-python/examples/pride.txt">Pride and Prejudice</a> from Project Gutenburg with my example code. You can click this link and copy/paste the text into a new text file called 'pride.txt' and save it in the same folder as your code</p> |
| 30 | + </section> |
| 31 | + |
| 32 | + <!-- Block 3 20 minutes --> |
| 33 | + <section> |
| 34 | + <h3>Lists of ... Lists</h3> |
| 35 | + <p>Lists can contain not only numbers or strings, but also other lists.</p> |
| 36 | + <p class="fragment">Such a structure is said to be <strong>nested</strong></p> |
| 37 | + <p class="fragment">The following is a list of lists:</p> |
| 38 | + <pre><code contenteditable class="fragment python"> |
| 39 | +game_board = [ |
| 40 | + ['O', 'X', ' '], |
| 41 | + [' ', 'X', ' '], |
| 42 | + [' ', ' ', ' '], |
| 43 | +] |
| 44 | + </code></pre> |
| 45 | + <p class="fragment">This can be indexed successively with <code>game_board[0][1]</code></p> |
| 46 | + </section> |
| 47 | + <section> |
| 48 | + <h3>Nested Lists continued</h3> |
| 49 | + <p>A list can be appended to a list as well</p> |
| 50 | + <pre><code contenteditable class="fragment python"> |
| 51 | +mary_to_dos = ['eat', 'work', 'pick up laundry', 'care for baby', 'sleep'] |
| 52 | +fran_to_dos = ['eat', 'work', 'call plumber', 'sleep'] |
| 53 | +baby_to_dos = ['eat', 'sleep'] |
| 54 | + |
| 55 | +all_to_dos = [] |
| 56 | +all_to_dos.append(mary_to_dos) |
| 57 | +all_to_dos.append(fran_to_dos) |
| 58 | +all_to_dos.append(baby_to_dos) |
| 59 | + |
| 60 | +print all_to_dos |
| 61 | + |
| 62 | +for to_dos in all_to_dos: |
| 63 | + for to_do in to_dos: |
| 64 | + print to_do |
| 65 | + </code></pre> |
| 66 | + <p class="fragment">What if we want to <strong>flatten</strong> the to do's?</p> |
| 67 | + <p class="fragment">What if we want the to do's to be unique?</p> |
| 68 | + </section> |
| 69 | + |
| 70 | + <section> |
| 71 | + <h3>Lists of Dictionaries</h3> |
| 72 | + <p>One of the most common and useful nested data structures, is a list of dictionaries</p> |
| 73 | + <pre><code contenteditable class="fragment python"> |
| 74 | +card_a = { |
| 75 | + 'suit': 'spades', |
| 76 | + 'number': 4, |
| 77 | +} |
| 78 | + |
| 79 | +card_b = { |
| 80 | + 'suit': 'hearts', |
| 81 | + 'number': 8, |
| 82 | +} |
| 83 | + |
| 84 | +hand = [card_a, card_b] |
| 85 | + |
| 86 | +print 'The hand contains:' |
| 87 | +for card in hand: |
| 88 | + print 'A', card['number'], 'of', card['suit'] |
| 89 | + </code></pre> |
| 90 | + </section> |
| 91 | + |
| 92 | + <section> |
| 93 | + <h3>Dictionary of Lists</h3> |
| 94 | + <p>A dictionary can also contain values that are themselves other collections, such as lists.</p> |
| 95 | + <p class="fragment">Let's revisit the group of to do lists and find a better representation:</p> |
| 96 | + <pre><code contenteditable class="fragment python"> |
| 97 | +mary_to_dos = ['eat', 'work', 'pick up laundry', 'care for baby', 'sleep'] |
| 98 | +fran_to_dos = ['eat', 'work', 'call plumber', 'sleep'] |
| 99 | +baby_to_dos = ['eat', 'sleep'] |
| 100 | + |
| 101 | +all_to_dos = { |
| 102 | + 'mary': mary_to_dos, |
| 103 | + 'fran': fran_to_dos, |
| 104 | + 'baby': baby_to_dos, |
| 105 | +} |
| 106 | + |
| 107 | +for name, to_dos in all_to_dos.items(): |
| 108 | + print name, 'needs to: ', to_dos |
| 109 | + |
| 110 | +# Changing this later can be accomplished with |
| 111 | +all_to_dos['baby'].append('cry') |
| 112 | + </code></pre> |
| 113 | + <p class="fragment">Now the to do lists can be indexed or modified by name</p> |
| 114 | + </section> |
| 115 | + |
| 116 | + <section> |
| 117 | + <h3>Means of Combination</h3> |
| 118 | + <p>Lists, dictionaries, sets, tuples, and other collections are all a means of combination.</p> |
| 119 | + <p class="fragment">They can be freely combined to create the data structure needed for a particular problem</p> |
| 120 | + <p class="fragment">Eg. A list of dictionaries with lists</p> |
| 121 | + <pre><code contenteditable class="fragment python"> |
| 122 | +all_tweets = [ |
| 123 | + { |
| 124 | + 'author': 'mary', |
| 125 | + 'handle': '@hadalittlelamb', |
| 126 | + 'date': '2013-01-22', |
| 127 | + 'tweets': [ |
| 128 | + 'at Loco Pops enjoying a Raspberry Sage popsicle', |
| 129 | + 'Learning Python is so much fun', |
| 130 | + ], |
| 131 | + }, |
| 132 | +] |
| 133 | + </code></pre> |
| 134 | + </section> |
| 135 | + |
| 136 | + <section> |
| 137 | + <h3>Builtins for collections</h3> |
| 138 | + <p>Python provides several functions that help us work with these collections.</p> |
| 139 | + <table class="fragment"> |
| 140 | + <tr> |
| 141 | + <td>len()</td> |
| 142 | + <td>Given a collection, return its length</td> |
| 143 | + </tr> |
| 144 | + <tr> |
| 145 | + <td>range()</td> |
| 146 | + <td>Create a list of integers in the range provided.</td> |
| 147 | + </tr> |
| 148 | + <tr> |
| 149 | + <td>sorted()</td> |
| 150 | + <td>Given a collection, returns a sorted copy of that collection</td> |
| 151 | + </tr> |
| 152 | + <tr> |
| 153 | + <td>enumerate()</td> |
| 154 | + <td>Returns a list of (index, element) from the list</td> |
| 155 | + </tr> |
| 156 | + <tr> |
| 157 | + <td>zip()</td> |
| 158 | + <td>Given one or more iterables, returns a list of tuples with an element from each iterable</td> |
| 159 | + </tr> |
| 160 | + </table> |
| 161 | + </section> |
| 162 | + |
| 163 | + <section> |
| 164 | + <h3>Examples of using Builtins</h3> |
| 165 | + <pre><code contenteditable class="fragment python"> |
| 166 | +# Using len() - Determines length |
| 167 | +print len([1, 2]) |
| 168 | + |
| 169 | +# range() - Quickly creates a list of integers |
| 170 | +print range(5) |
| 171 | +print range(5, 10) |
| 172 | +print range(0, 10, 2) |
| 173 | +print range(9, -1, -1) |
| 174 | + |
| 175 | +# sorted() - Sort a given list |
| 176 | +grades = [93, 100, 60] |
| 177 | +grades = sorted(grades) |
| 178 | +print grades |
| 179 | + </code></pre> |
| 180 | + </section> |
| 181 | + |
| 182 | + <section> |
| 183 | + <h3>Builtins Examples continued</h3> |
| 184 | + <pre><code contenteditable class="fragment python"> |
| 185 | +# enumerate() - Obtain the index of the element in the loop |
| 186 | + |
| 187 | +print 'To Do:' |
| 188 | +to_dos = ['work', 'sleep', 'work'] |
| 189 | +for index, item in enumerate(to_dos): |
| 190 | + print '{0}. {1}'.format(index + 1, item) |
| 191 | + |
| 192 | +print list(enumerate(to_dos)) |
| 193 | + |
| 194 | +# zip() |
| 195 | +widths = [10, 15, 20] |
| 196 | +heights = [5, 8, 12] |
| 197 | +for width, height in zip(widths, heights): |
| 198 | + print 'Area is {0}'.format(width * height) |
| 199 | + </code></pre> |
| 200 | + </section> |
| 201 | + |
| 202 | + <!-- Let's develop it: 25 minutes --> |
| 203 | + <section> |
| 204 | + <h3>Let's Develop It</h3> |
| 205 | + <p>Write a program that expands on your previous one. If it is unfinished, feel free to finish the original exercise first. To expand on it, choose one of the following:</p> |
| 206 | + <ol> |
| 207 | + <li>Determine how many times the user provided word appears in the file and/or what lines it appears on</li> |
| 208 | + <li>Change the program so that it counts the number of times each word occurs. E.g. A dictionary of all words in the file, whose values are a count of their occurrences</li> |
| 209 | + <li>Use <a href="http://calebsmith.github.io/gdi-intro-python/examples/boilerplate.py">boilerplate.py</a> to help you improve the reusability of the program. (The comments in that file should explain the how and why)</li> |
| 210 | + </ol> |
| 211 | + <p>Resources for this and the previous exercise are provided on the next slide for convenience</p> |
| 212 | + </section> |
| 213 | + |
| 214 | + <section> |
| 215 | + <h3>Resources</h3> |
| 216 | + <ul> |
| 217 | + <li>Helper functions are in <a href="http://calebsmith.github.io/gdi-intro-python/examples/helpers.py">helpers.py</a></li> |
| 218 | + <li>Download a book in plain text from <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenburg</a> and put it into the same directory as your python file.</li> |
| 219 | + <li>You can use this link for <a href="http://calebsmith.github.io/gdi-intro-python/examples/pride.txt">Pride and Prejudice</a>. Click this link and copy/paste the text into a new text file called 'pride.txt' and save it in the same folder as your code</li> |
| 220 | + |
| 221 | + </ul> |
| 222 | + <pre><code contenteditable class="python"> |
| 223 | +from helpers import generate_cleaned_lines |
| 224 | + |
| 225 | +def is_word_in_file(word, filename): |
| 226 | + for line in generate_cleaned_lines(filename): |
| 227 | + # line will be a string of each line of the file in order |
| 228 | + # Your code goes here. Do something with the word and line variables |
| 229 | + return result |
| 230 | + |
| 231 | +input_word = raw_input("Enter a word to search for:") |
| 232 | +answer = is_word_in_file(input_word, 'pride.txt') |
| 233 | +# Display the answer in some meaningful way |
| 234 | + </code></pre> |
| 235 | + </section> |
| 236 | + |
| 237 | + <section> |
| 238 | + <h3>Questions?</h3> |
| 239 | + </section> |
0 commit comments