|
| 1 | + |
| 2 | +###Intro to Python |
| 3 | +####Class 4 |
| 4 | +@@@ |
| 5 | + |
| 6 | +###Review |
| 7 | + |
| 8 | +* Method calls |
| 9 | + |
| 10 | +* Combining lists and dictionaries |
| 11 | + |
| 12 | +* Builtins for collections |
| 13 | + |
| 14 | +@@@ |
| 15 | + |
| 16 | +###Functions on Dictionaries |
| 17 | + |
| 18 | +```python |
| 19 | +character = { |
| 20 | + 'x': 10, |
| 21 | + 'y': 20, |
| 22 | + 'health': 100, |
| 23 | +} |
| 24 | +def injure(character, damage): |
| 25 | + character['health'] = character['health'] - damage |
| 26 | + if character['health'] < 0: |
| 27 | + character['health'] = 0 |
| 28 | +def heal(character, amount): |
| 29 | + character['health'] = character['health'] + amount |
| 30 | + if character['health'] > 100: |
| 31 | + character['health'] = 100 |
| 32 | +``` |
| 33 | +Note: Block 1 - Intro to Classes and OOP - 20 minutes |
| 34 | +@@@ |
| 35 | + |
| 36 | +###Classes |
| 37 | +A <strong>class</strong> creates a new type of object. |
| 38 | + |
| 39 | +A class defines the attributes and methods of objects of that type |
| 40 | + |
| 41 | +Classes are used to create new objects of that type |
| 42 | + |
| 43 | +```python |
| 44 | +class Character(): |
| 45 | + def __init__(self, x, y, health): |
| 46 | + self.x = x |
| 47 | + self.y = y |
| 48 | + self.health = health |
| 49 | +character = Character(10, 20, 100) |
| 50 | +``` |
| 51 | +@@@ |
| 52 | + |
| 53 | +###A Sense of Self |
| 54 | +The first argument to every method is <strong>self</strong>. |
| 55 | + |
| 56 | +self contains the attributes and methods for the current object |
| 57 | + |
| 58 | +```python |
| 59 | +class Character(): |
| 60 | + def __init__(self, x, y, health): |
| 61 | + self.x = x |
| 62 | + self.y = y |
| 63 | + self.health = health |
| 64 | +character = Character(10, 20, 100) |
| 65 | +``` |
| 66 | +@@@ |
| 67 | + |
| 68 | +###The __init__ Method |
| 69 | +This method defines what the class should do when creating a new object. |
| 70 | + |
| 71 | +```python |
| 72 | +class Character(): |
| 73 | + def __init__(self, x, y, health): |
| 74 | + self.x = x |
| 75 | + self.y = y |
| 76 | + self.health = health |
| 77 | +character_a = Character(10, 20, 100) |
| 78 | +character_b = Character(10, 20, 100) |
| 79 | +``` |
| 80 | +To create a new Character, the syntax looks like a function call. These arguments are passed to the __init__ method |
| 81 | + |
| 82 | +@@@ |
| 83 | + |
| 84 | +###Class Methods |
| 85 | +A class also defines <strong>methods</strong>, which are functions that operate on objects of that type |
| 86 | + |
| 87 | +Assigning values to an attribute on self is how we <strong>mutate</strong> the object's state. |
| 88 | + |
| 89 | +```python |
| 90 | +# inside the character class |
| 91 | + def heal(self, amount): |
| 92 | + self.health = self.health + amount |
| 93 | + if self.health > 100: |
| 94 | + self.health = 100 |
| 95 | + def injure(self, amount): |
| 96 | + self.health = self.health - amount |
| 97 | + if self.health < 0: |
| 98 | + self.health = 0 |
| 99 | +character = Character(10, 20, 100) |
| 100 | +character.injure(10) |
| 101 | +``` |
| 102 | +@@@ |
| 103 | + |
| 104 | +###Let's Develop It |
| 105 | + |
| 106 | +* In your text editor, create your own class with an __init__ method, and at least one other method. |
| 107 | + |
| 108 | +* Open a Python shell and import the class. Create one or more objects from the class |
| 109 | + |
| 110 | +* If time allows, create a function that creates objects from your class, calls a method, and prints one of its attributes |
| 111 | + |
| 112 | +* Use the next slide as an example |
| 113 | + |
| 114 | +Note: Let's develop it: 10 minutes |
| 115 | +@@@ |
| 116 | + |
| 117 | +```python |
| 118 | +# in character.py |
| 119 | +class Character(): |
| 120 | + def __init__(self, x, y, health): |
| 121 | + self.x = x |
| 122 | + self.y = y |
| 123 | + self.health = health |
| 124 | + def heal(self, amount): |
| 125 | + self.health = self.health + amount |
| 126 | + if self.health > 100: |
| 127 | + self.health = 100 |
| 128 | +``` |
| 129 | +```python |
| 130 | +# in Python shell |
| 131 | +from character import Character |
| 132 | +character_a = Character(10, 20, 100) |
| 133 | +character_a.injure(10) |
| 134 | +print "character health is: " + character_a.health |
| 135 | +``` |
| 136 | +@@@ |
| 137 | + |
| 138 | +###Inheritance |
| 139 | +A class can <strong>inherit</strong> from another class. |
| 140 | + |
| 141 | +A class that inherits from another is called the "child class" and obtains the methods and attributes of its "parent" |
| 142 | + |
| 143 | +```python |
| 144 | +class Mobile(object): |
| 145 | + """ |
| 146 | + An object with an x, y position, and methods for moving |
| 147 | + """ |
| 148 | + def __init__(self, x, y): |
| 149 | + self.x = x |
| 150 | + self.y = y |
| 151 | + def move_up(): |
| 152 | + self.y = self.y - 1 |
| 153 | + # ... methods for move_down, move_left, and move_right |
| 154 | +``` |
| 155 | +Note: Block 2 - Inheritance and Composition 20 Minutes |
| 156 | +@@@ |
| 157 | + |
| 158 | +###Inheritance Continued |
| 159 | +The move_up method is <strong>overridden</strong> in the child class below: |
| 160 | + |
| 161 | +```python |
| 162 | +class BoundedMobile(Mobile): |
| 163 | + """ |
| 164 | + An object with an x, y position, and methods for moving |
| 165 | + The x, y position must be within bounds |
| 166 | + """ |
| 167 | + def move_up(): |
| 168 | + self.y = self.y - 1 |
| 169 | + if self.y < 0: |
| 170 | + self.y = 0 |
| 171 | +``` |
| 172 | +See [mobile.py](http://calebsmith.github.io/gdi-intro-python/examples/mobile.py) for a more complete example. |
| 173 | + |
| 174 | +@@@ |
| 175 | + |
| 176 | +###What's Super about Super |
| 177 | +<strong>super</strong> is often helpful when writing methods that override the method of the parent class |
| 178 | + |
| 179 | +```python |
| 180 | +class BoundedMobile(Mobile): |
| 181 | + """ |
| 182 | + An object with an x, y position, and methods for moving |
| 183 | + The x, y position must be within bounds |
| 184 | + """ |
| 185 | + def move_up(): |
| 186 | + super(BoundedMobile, self).move_up() |
| 187 | + if self.y < 0: |
| 188 | + self.y = 0 |
| 189 | +``` |
| 190 | +The call to super() takes the name of the child class, followed by self. This is followed by the method call and any arguments to pass to it |
| 191 | + |
| 192 | +@@@ |
| 193 | + |
| 194 | +###Composition |
| 195 | +Classes can also use the technique of <strong>composition</strong> |
| 196 | + |
| 197 | +This simply means that a given object contains other objects within it. |
| 198 | + |
| 199 | +This often leads to a clearer and simpler design |
| 200 | + |
| 201 | +```python |
| 202 | +class Grid(object): |
| 203 | + def __init__(self, x_limit, y_limit): |
| 204 | + self.x_limit = x_limit |
| 205 | + self.y_limit = y_limit |
| 206 | + self.mobiles = [] |
| 207 | + def add_mobile(self, x, y): |
| 208 | + mob = BoundedMobile(x, y, self.x_limit, self.y_limit) |
| 209 | + mobs = self.mobiles.get((x, y), []) |
| 210 | + mobs.append(mob) |
| 211 | + self.mobiles[(x, y)] = mobs |
| 212 | +``` |
| 213 | +@@@ |
| 214 | + |
| 215 | +###Composition Continued |
| 216 | +Given the class on the previous slide, the following code creates mobiles within the grid object. (Complete code is available in the aforementioned [mobile.py](http://calebsmith.github.io/gdi-intro-python/examples/mobile.py)) |
| 217 | + |
| 218 | +```python |
| 219 | +from mobile import Grid |
| 220 | +grid = Grid(7, 7) |
| 221 | +grid.add_mobile(1, 2) |
| 222 | +grid.add_mobile(0, 1) |
| 223 | +grid.add_mobile(0, 1) |
| 224 | +grid.display_grid() |
| 225 | +``` |
| 226 | +@@@ |
| 227 | + |
| 228 | +###Let's Develop It |
| 229 | +Create a class that uses inheritance, composition, or both. |
| 230 | + |
| 231 | +To help you, use your work from the last exercise or the classes from [mobile.py](http://calebsmith.github.io/gdi-intro-python/examples/mobile.py) |
| 232 | + |
| 233 | +Note: Block 3 - Functional Programming - 20 Minutes |
| 234 | +@@@ |
| 235 | + |
| 236 | +###List Comprehensions |
| 237 | +I have a list and I have something I want to do to each element to create a new list |
| 238 | + |
| 239 | +```python |
| 240 | +squares = [] |
| 241 | +for number in range(11): |
| 242 | + squares.append(number ** 2) |
| 243 | +``` |
| 244 | +This pattern is so common, there is a shorter way to express it: |
| 245 | + |
| 246 | +```python |
| 247 | +squares = [number ** 2 for number in range(11)] |
| 248 | +``` |
| 249 | +This is called a <strong>list comprehension</strong> |
| 250 | +Note: Let's develop it: 10 minutes |
| 251 | + |
| 252 | +@@@ |
| 253 | + |
| 254 | +###Generators |
| 255 | +A <strong>generator</strong> is like a list, but it is evaluated when it is used rather than when it is defined. |
| 256 | + |
| 257 | +Generators are useful when the list may be very large or when the task is time consuming |
| 258 | + |
| 259 | +```python |
| 260 | +#Define a generator |
| 261 | +def find_squares(numbers): |
| 262 | + for number in numbers |
| 263 | + yield number ** 2 |
| 264 | +#Iterate on a generator like a list |
| 265 | +for square in find_squares(range(10)): |
| 266 | + print square |
| 267 | +``` |
| 268 | +A function with a <strong>yield</strong> statement creates a generator. Each yield statement "yields" the next value in the sequence |
| 269 | + |
| 270 | +@@@ |
| 271 | + |
| 272 | +###Generator Comprehensions |
| 273 | +A <strong>generator comprehension</strong> is created the same way as list comprehension, but replacing the square brackets with parenthesis. |
| 274 | + |
| 275 | +```python |
| 276 | +ten_squares = (number ** 2 for number in range(10)) |
| 277 | +``` |
| 278 | + |
| 279 | +These are used liberally in the functions of [books.py](http://calebsmith.github.io/gdi-intro-python/examples/books.py) example from class 3. |
| 280 | + |
| 281 | +@@@ |
| 282 | + |
| 283 | +###Higher order functions |
| 284 | +A <strong>higher order function</strong> is a function that returns a function, takes a function as an argument, or both |
| 285 | + |
| 286 | +One commonly used higher order function that is a Python builtin is called <strong>map</strong> |
| 287 | + |
| 288 | +```python |
| 289 | +# Define any function |
| 290 | +def sqaure(number): |
| 291 | + return number ** 2 |
| 292 | +# Pass the function to map along with an iterable |
| 293 | +squares = map(square, range(10)) |
| 294 | +``` |
| 295 | +@@@ |
| 296 | + |
| 297 | +###Let's Develop It |
| 298 | + |
| 299 | +Choose among any of these projects (Resources available on the next page): |
| 300 | + |
| 301 | +* Search the Web - Write a program that searches the web using DuckDuckGo and displays results. |
| 302 | + |
| 303 | +* Encryption - Write a program that encrypts a string from user input, or file and is able to decrypt it as well. |
| 304 | + |
| 305 | +* Command Line Game - Create a simple game that runs inside the terminal. |
| 306 | +@@@ |
| 307 | + |
| 308 | +###Let's Develop It Resources |
| 309 | +<table> |
| 310 | + <tr> |
| 311 | + <td>Search the Web</td> |
| 312 | + <td>Use the [python-duckduckgo](https://github.com/crazedpsyc/python-duckduckgo/) library to get started. Download duckduckgo.py and put it in the same directory as your code. Use the query() function it provides to begin. (HINT: Results are often empty, but 'related' list usually has a few hits.)</td> |
| 313 | + </tr> |
| 314 | + <tr> |
| 315 | + <td>Encryption</td> |
| 316 | + <td>Read about the [Caesar Cipher](https://en.wikipedia.org/wiki/Caesar_cipher) or find a similarly simple encryption mechanism online. You should find the ord() and chr() functions helpful, as well as the modulus operator '%'</td> |
| 317 | + </tr> |
| 318 | +</table> |
| 319 | +continued on next page... |
| 320 | + |
| 321 | +@@@ |
| 322 | + |
| 323 | +###Let's Develop It Resources Continued |
| 324 | + |
| 325 | +<table> |
| 326 | + <tr> |
| 327 | + <td>Command Line Game</td> |
| 328 | + <td>This might be a text adventure with paragraphs of text followed by a series of choices for the user. A choice maps to another node in the story (another paragraph with choices). You might try storing the paragraphs separately in a text file. The format might be something different, such as a series of "rooms", each with a description, for the user to explore by entering commands such as "go west". Examples of these kinds of games are [Colossal Cave Adventure](https://en.wikipedia.org/wiki/Colossal_Cave_Adventure) and <a href="https://en.wikipedia.org/wiki/Zor">Zork</a></td> |
| 329 | + </tr> |
| 330 | +</table> |
| 331 | +@@@ |
| 332 | + |
| 333 | +###Future Resources |
| 334 | + |
| 335 | +<table> |
| 336 | + <tr> |
| 337 | + <td>[Python.org Documentation](http://docs.python.org/2/)</td> |
| 338 | + <td>Official Python Documentation</td> |
| 339 | + </tr> |
| 340 | + <tr> |
| 341 | + <td>[Think Python](http://www.greenteapress.com/thinkpython/html/index.html)</td> |
| 342 | + <td>Online and print book with exercises.</td> |
| 343 | + </tr> |
| 344 | + <tr> |
| 345 | + <td>[Learn Python the Hard Way](http://learnpythonthehardway.org/book/)</td> |
| 346 | + <td>Online and print book with exercises</td> |
| 347 | + </tr> |
| 348 | + <tr> |
| 349 | + <td>[Google's Python Class](https://developers.google.com/edu/python/)</td> |
| 350 | + <td>Video lectures coupled with exercises</td> |
| 351 | + </tr> |
| 352 | + <tr> |
| 353 | + <td>[New Coder](http://newcoder.io/tutorials/)</td> |
| 354 | + <td>Ideas for slightly larger projects and resources to get you started. Projects include accessing API's, scraping pages, writing IRC bots, and others.</td> |
| 355 | + </tr> |
| 356 | + <tr> |
| 357 | + <td>[Girl Develop It](http://girldevelopit.com/)</td> |
| 358 | + <td>Local workshops, events, and coding sessions</td> |
| 359 | + </tr> |
| 360 | +</table> |
| 361 | +@@@ |
| 362 | + |
| 363 | +###Questions? |
0 commit comments