|
| 1 | +# Practical Python Programming - Instructor Notes |
| 2 | + |
| 3 | +Author: David Beazley |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This document provides some general notes and advice on teaching the |
| 8 | +content of my “Practical Python” course including objectives, target |
| 9 | +audience, tricky bits, etc. |
| 10 | + |
| 11 | +These instructions were given to people teaching the course in |
| 12 | +a typical three-day corporate training environment. They might |
| 13 | +give you some insight about teaching your own course. |
| 14 | + |
| 15 | +## Target Audience and General Approach |
| 16 | + |
| 17 | +This course is intended to be an “Introduction to Python” course for |
| 18 | +people who already have some programming experience. This is |
| 19 | +definitely not a course designed to teach people “programming 101.” |
| 20 | + |
| 21 | +Having said that, I have observed that the typical student in a Python |
| 22 | +course is also not likely to be a hard-core software engineer or |
| 23 | +programmer. Instead, you are probably going to get a mix of |
| 24 | +engineers, scientists, web programmers, and more inexperienced |
| 25 | +developers. Student background varies widely. You might have some |
| 26 | +students with a lot of C,C++, Java experience, others might know PHP |
| 27 | +and HTML, others may be coming from tools like MATLAB, and others |
| 28 | +still might have almost no traditional “programming” experience at all |
| 29 | +despite my best attempts to make the prerequisites clear. |
| 30 | + |
| 31 | +With this in mind, the course aims to teach Python through the general |
| 32 | +problem of manipulating data (stock market data in particular). This |
| 33 | +domain has been chosen because it’s simple and something everyone |
| 34 | +should know about it regardless of their background. Just as an example, |
| 35 | +students with weak programming skills are still likely to know about |
| 36 | +common things like using a spreadsheet (e.g., Excel). So, if they’re |
| 37 | +really stuck, you can tell them things like “well, this list of tuples |
| 38 | +is kind of like rows of data in a spreadsheet” or “a list |
| 39 | +comprehension is the same idea as applying an operation to a |
| 40 | +spreadsheet column and putting the result in a different column.” The |
| 41 | +key idea is to stay grounded in a real-world setting as opposed to |
| 42 | +getting sidetracked into esoteric “computer science” problems (e.g., |
| 43 | +“let’s go compute fibonacci numbers.”). |
| 44 | + |
| 45 | +This problem domain also works well for introducing other programming |
| 46 | +topics. For example, scientists/engineers might want to know about |
| 47 | +data analysis or plotting. So, you can show them how to make a plot |
| 48 | +using matplotlib. Web programmers might want to know how to present |
| 49 | +stock market data on a web-page. So, you can talk about template |
| 50 | +engines. Syadmins might want to do something with log files. So, you |
| 51 | +can point them at a log file of real-time streaming stock data. |
| 52 | +Software engineers might want to know about design. So, you can have |
| 53 | +them look at ways to encapsulate stock data inside an object or making |
| 54 | +a program extensible (e.g., how would make this program produce output |
| 55 | +in 10 different table formats). You get the idea. |
| 56 | + |
| 57 | +## Presentation Guidelines |
| 58 | + |
| 59 | +The presentation slides (notes) are there to provide a narrative |
| 60 | +structure to the course and for reference by students when they work |
| 61 | +on exercises. Do not laboriously go over every bullet point on every |
| 62 | +slide--assume that students can read and that they will have time to |
| 63 | +go back when coding. I tend to go through the slides at a pretty |
| 64 | +brisk pace, showing short examples interactively as I go. I often |
| 65 | +skip slides entirely in favor of live demos. For example, you don't |
| 66 | +really need to do a bunch of slides on lists. Just go to the |
| 67 | +interpreter and do some list examples live instead. Rule of thumb: No |
| 68 | +more than 1 minute per slide unless it’s something unusually tricky. |
| 69 | +Honestly, you could probably skip most of the slides and simply |
| 70 | +lecture using live demos if you feel that it works for you. I often |
| 71 | +do this. |
| 72 | + |
| 73 | +## Course Exercises |
| 74 | + |
| 75 | +The course has about 130 hands-on exercises. If you do every single |
| 76 | +exercise and give students time to think and code, it will likely take |
| 77 | +them about 10-12 hours. In practice, you will probably find that students |
| 78 | +require more time on certain exercises. I have some notes about this |
| 79 | +below. |
| 80 | + |
| 81 | +You should repeatedly emphasize to students that solution code is |
| 82 | +available and that it is okay to look at it and copy it--especially |
| 83 | +due to time requirements. |
| 84 | + |
| 85 | +Prior to teaching the course, I would strongly advise that you go |
| 86 | +through and work every single course exercise so that there are no |
| 87 | +surprises. |
| 88 | + |
| 89 | +During course delivery, I usually work every single exercise from |
| 90 | +scratch, without looking at the solution, on my computer while the |
| 91 | +students also work. For this, I strongly advise you to have a printed |
| 92 | +copy of the exercises on hand that you can look at without having to |
| 93 | +pull it up on the computer screen (which is being projected). Near |
| 94 | +the end of the exercise time period, I will start discussing my |
| 95 | +solution code, emphasizes different bits on the screen and talking |
| 96 | +about them. If there are any potential problems with the solution |
| 97 | +(including design considerations), I’ll also talk about it. Emphasize |
| 98 | +to students that they may want to look at/copy solution code before |
| 99 | +going forward. |
| 100 | + |
| 101 | +## Section 1: Introduction |
| 102 | + |
| 103 | +The major goal of this section is to get people started with the |
| 104 | +environment. This includes using the interactive shell and |
| 105 | +editing/run short programs. By the end of the section, students |
| 106 | +should be able to write short scripts that read data files and perform |
| 107 | +small calculations. They will know about numbers, strings, lists, and |
| 108 | +files. There will also be some exposure to functions, exceptions, and |
| 109 | +modules, but a lot of details will be missing. |
| 110 | + |
| 111 | +The first part of this course is often the longest because students |
| 112 | +are new to the tools and may have various problems getting things to |
| 113 | +work. It is absolutely critical that you go around the room and make |
| 114 | +sure that everyone can edit, run, and debug simple programs. Make |
| 115 | +sure Python is installed correctly. Make sure they have the course |
| 116 | +exercises downloaded. Make sure the internet works. Fix anything |
| 117 | +else that comes up. |
| 118 | + |
| 119 | +Timing: I aim to finish section 1 around lunch on the first day. |
| 120 | + |
| 121 | +## Section 2 : Working with Data |
| 122 | + |
| 123 | +This section is probably the most important in the course. It covers |
| 124 | +the basics of data representation and manipulation including tuples, |
| 125 | +lists, dicts, and sets. |
| 126 | + |
| 127 | +Section 2.2 the most important. Give students as much time as |
| 128 | +they need to get exercises working within reason. Depending on audience, |
| 129 | +the exercises might last 45 minutes. In the middle of this exercise, |
| 130 | +I will often move forward to Section 2.3 (formatted printing) and |
| 131 | +give students more time to keep working. Together, Sections 2.2/2.3 |
| 132 | +might take an hour or more. |
| 133 | + |
| 134 | +Section 2.4 has people explore the use of enumerate(), and zip(). I |
| 135 | +consider these functions essential so don’t skimp on it. |
| 136 | + |
| 137 | +Section 2.5 introduces the collections module. There is a LOT that |
| 138 | +could be said about collections, but it won't be fully appreciated by |
| 139 | +students at this time. Approach this more from the standpoint of |
| 140 | +"here's this cool module you should look at later. Here are a few cool |
| 141 | +examples." |
| 142 | + |
| 143 | +Section 2.6 introduces list comprehensions which are an important |
| 144 | +feature for processing list data. Emphasize to students that list |
| 145 | +comprehensions are very similar to things like SQL database queries. |
| 146 | +At the end of this exercise, I often do an interactive demo involving |
| 147 | +something more advanced. Maybe do a list comprehension and plot some |
| 148 | +data with matplotlib. Also an opportunity to introduce Jupyter if |
| 149 | +you're so inclined. |
| 150 | + |
| 151 | +Section 2.7 is the most sophisticated exercise. It relates to the use |
| 152 | +of first-class data in Python and the fact that data structures like |
| 153 | +lists can hold any kind of object that you want. The exercises are |
| 154 | +related to parsing columns of data in CSV files and concepts are later reused in |
| 155 | +Section 3.2. |
| 156 | + |
| 157 | +Timing: Ideally, you want to be done with section 2 on the first day. |
| 158 | +However, it is common to finish with section 2.5 or 2.6. So, don't |
| 159 | +panic if you feel that you're a bit behind. |
| 160 | + |
| 161 | +## 3. Program Organization |
| 162 | + |
| 163 | +The main goal of this section is to introduce more details about |
| 164 | +functions and to encourage students to use them. The section builds |
| 165 | +from functions into modules and script writing. |
| 166 | + |
| 167 | +Section 3.1 is about going from simple “scripting” to functions. |
| 168 | +Students should be discouraged from writing disorganized “scripts.” |
| 169 | +Instead, code should at least be modularized into functions. It makes |
| 170 | +the code easier to understand, it makes it easier to make changes |
| 171 | +later, and it actually runs a little bit faster. Functions are good. |
| 172 | + |
| 173 | +Section 3.2 is probably the most advanced set of exercises in the |
| 174 | +whole course. It has students write a general purpose utility |
| 175 | +function for parsing column-oriented data. However, it makes heavy |
| 176 | +use of list comprehensions as well as lists of functions (e.g., |
| 177 | +functions as first-class objects). You will probably need to guide |
| 178 | +people through every single step of this code, showing how it works in |
| 179 | +great detail. The payoff is huge however---you can show people a |
| 180 | +short general purpose function that does something amazingly powerful |
| 181 | +and which would be virtually impossible to write in C, C++, or Java |
| 182 | +without having a *LOT* of very complicated code. There are a lot of |
| 183 | +possible design/discussion avenues for this code. Use your |
| 184 | +imagination. |
| 185 | + |
| 186 | +Section 3.3 adds error handling to the function created in Section 3.2 |
| 187 | +This is a good time to talk about exception handling generally. |
| 188 | +Definitely talk about the dangers of catching all exceptions. This |
| 189 | +might be a good time to talk about the “Errors should never pass |
| 190 | +silently” item on the “Zen of Python.” |
| 191 | + |
| 192 | +*Note: Before Exercise 3.4, make sure students get fully working versions of report.py, pcost.py, and fileparse.py. Copy from Solutions folder if needed * |
| 193 | + |
| 194 | +Section 3.4 Introduces module imports. The file written in Section |
| 195 | +3.2-3.3 is used to simplify code in Section 3.1. Be aware that you |
| 196 | +may need to help students fix issues with IDLE, sys.path, and other |
| 197 | +assorted settings related to import. |
| 198 | + |
| 199 | +Section 3.5 talks about __main__ and script writing. There's a bit |
| 200 | +about command line arguments. You might be inclined to discuss a |
| 201 | +module like argparse. However, be warned that doing so opens up |
| 202 | +a quagmire. It's usually better to just mention it and move on. |
| 203 | + |
| 204 | +Section 3.6 opens up a discussion about design more generally in Python. |
| 205 | +Is it better to write code that's more flexible vs code that's |
| 206 | +hardwired to only work with filenames? This is the first place |
| 207 | +where you make a code change and have to refactor existing code. |
| 208 | + |
| 209 | +Going forward from here, most of the exercises make small changes |
| 210 | +to code that's already been written. |
| 211 | + |
| 212 | +## 4. Classes and Objects |
| 213 | + |
| 214 | +This section is about very basic object oriented programming. In |
| 215 | +general, it is not safe to assume that people have much background in |
| 216 | +OO. So, before starting this, I usually generally describe the OO |
| 217 | +“style” and how it's data and methods bundled together. Do some |
| 218 | +examples with strings and lists to illustrate that they are “objects” |
| 219 | +and that the methods (invoked via .) do things with the object. |
| 220 | +Emphasize how the methods are attached to the object itself. For |
| 221 | +example, you do items.append(x), you don’t call a separate function |
| 222 | +append(items, x). |
| 223 | + |
| 224 | +Section 4.1 introduces the class statement and shows people how to |
| 225 | +make a basic object. Really, this just introduces classes as another |
| 226 | +way to define a simple data structure--relating back to using tuples |
| 227 | +and dicts for this purpose in section 2. |
| 228 | + |
| 229 | +Section 4.2 is about inheritance and how you use to create extensible |
| 230 | +programs. This set of exercises is probably the most significant in terms of |
| 231 | +OO programming and OO design. Give students a lot of time to work on |
| 232 | +it (30-45 minutes). Depending on interest, you can spend a LOT of |
| 233 | +time discussing aspects of OO. For example, different |
| 234 | +design patterns, inheritance hierarchies, abstract base classes, etc. |
| 235 | + |
| 236 | +Section 4.3 does a few experiments with special methods. I wouldn't |
| 237 | +spend too much time fooling around with this. Special methods come up |
| 238 | +a bit later in Exercise 6.1 and elsewhere. |
| 239 | + |
| 240 | +Timing: This is usually the end of the 2nd day. |
| 241 | + |
| 242 | +## 5. Inside Objects |
| 243 | + |
| 244 | +This section takes students behind the scenes of the object system and |
| 245 | +how it’s built using dictionaries, how instances and classes are tied |
| 246 | +together, and how inheritance works. However, most important part of |
| 247 | +this section is probably the material about encapsulation (private |
| 248 | +attributes, properties, slots, etc.) |
| 249 | + |
| 250 | +Section 5.1 just peels back the covers and has students observe and |
| 251 | +play with the underlying dicts of instances and classes. |
| 252 | + |
| 253 | +Section 5.2 is about hiding attributes behind get/set functions and |
| 254 | +using properties. I usually emphasize that these techniques are |
| 255 | +commonly used in libraries and frameworks--especially in situations |
| 256 | +where more control over what a user is allowed to do is desired. |
| 257 | + |
| 258 | +An astute Python master will notice that I do not talk about advanced |
| 259 | +topics such as descriptors, or attribute access methods (__getattr__, |
| 260 | +__setattr__) at all. I have found, through experience, that this is |
| 261 | +just too much mental overload for students taking the intro course. |
| 262 | +Everyone’s head is already on the verge of exploding at this point and |
| 263 | +if you go talk about how something like descriptors work, you’ll lose |
| 264 | +them for the rest of the day, if not the rest of the course. Save it |
| 265 | +for an "Advanced Python" course. |
| 266 | + |
| 267 | +If you're looking at the clock thinking "There's no way I'm going to |
| 268 | +finish this course", you can skip section 5 entirely. |
| 269 | + |
| 270 | +## 6. Generators |
| 271 | + |
| 272 | +The main purpose of this section is to introduce generators as a way |
| 273 | +to define custom iteration and to use them for various problems |
| 274 | +related to data handling. The course exercises have students analyze |
| 275 | +streaming data in the form of stock updates being written to a log |
| 276 | +file. |
| 277 | + |
| 278 | +There are two big ideas to emphasize. First, generators can be used to |
| 279 | +write code based on incremental processing. This can be very useful |
| 280 | +for things like streaming data or huge datasets that are too large to |
| 281 | +fit into memory all at once. The second idea is that you can chain |
| 282 | +generators/iterators together to create processing pipelines (kind of |
| 283 | +like Unix pipes). Again, this can be a really powerful way to process |
| 284 | +and think about streams, large datasets, etc. |
| 285 | + |
| 286 | +Some omissions: Although the iteration protocol is described, the |
| 287 | +notes don’t go into detail about creating iterable objects (i.e., |
| 288 | +classes with __iter__() and next()). In practice, I’ve found that |
| 289 | +it’s not necessary to do this so often (generators are often |
| 290 | +better/easier). So, in the interest of time, I’ve made a conscious |
| 291 | +decision to omit it. Also not included are extended generators |
| 292 | +(coroutines) or uses of generators for concurrency (tasklets, etc.). |
| 293 | +That’s better covered in advanced courses. |
| 294 | + |
| 295 | +## 7. Advanced Topics |
| 296 | + |
| 297 | +Basically this section is an assortment of more advanced topics that |
| 298 | +could have been covered earlier, but weren’t for various reasons |
| 299 | +related to course flow and content of the course exercises. If you |
| 300 | +must know, I used to present this material earlier in the course, but |
| 301 | +found that students were already overloaded with enough information. |
| 302 | +Coming back to it later seems to work better---especially since by |
| 303 | +this point, everyone is much more familiar with working in Python and |
| 304 | +starting to get the hang of it. |
| 305 | + |
| 306 | +Topics include variadic function arguments (*args, **kwargs), lambda, |
| 307 | +closures, and decorators. Discussion of decorators is only a tiny |
| 308 | +hint of what’s possible with metaprogramming. Feel free to say more |
| 309 | +about what’s possible, but I’d probably stay out of metaclasses! |
| 310 | +Lately, I have been demoing "numba" as an example of a more |
| 311 | +interesting decorator. |
| 312 | + |
| 313 | +If you're pressed for time, most of section 7 can be skipped or heavily |
| 314 | +compressed (you could skip exercises for instance). |
| 315 | + |
| 316 | +## 8. Testing and Debugging |
| 317 | + |
| 318 | +The main purpose of this section is just to introduce various tools |
| 319 | +and techniques related to testing, debugging, and software |
| 320 | +development. Show everyone the unittest module. Introduce the |
| 321 | +logging module. Discuss assertions and the idea of “contracts.” Show |
| 322 | +people the debugger and profiler. Most of this is self-explanatory. |
| 323 | + |
| 324 | +## 9. Packages |
| 325 | + |
| 326 | +At this point, students have written an assortment of files (pcost.py, |
| 327 | +report.py, fileparse.py, tableformat.py, stock.py, portfolio.py, |
| 328 | +follow.py, etc.). Two main goals in this section. First, put all of |
| 329 | +the code into a Python package structure. This is only a gentle |
| 330 | +introduction to that, but they'll move the files into a directory and |
| 331 | +everything will break. They'll need to fix their import statements |
| 332 | +(package relative imports) and maybe fiddle with an __init__.py file. |
| 333 | +Second goal, write a simple setup.py file that they can use to package |
| 334 | +up the code and give it away to someone. That's it. End of the |
| 335 | +course. |
| 336 | + |
| 337 | +[Contents](Contents.html) |
| 338 | + |
| 339 | + |
0 commit comments