Learning Python - many new example programs
Archive - Originally posted on "The Horse's Mouth" - 2009-01-31 10:22:01 - Graham EllisI ran a public Python course earlier this week ... and (as I usually do!) wrote a number of short examples during the class to show the delegates not only how something is done, but also how the coding process works and how the particular answers are developed. I can't show you that process here on the blog (I can on future Python courses) but I did promise my delegates that I would add some documentation to the code and post it for them ... so here goes ...
Day 1

[source] Lots of ways of manipulating a list - setting it with an array-like syntax, inserting elements, using slices and various ways of iterating through it. From some of the food products and mixtures that this demonstration uses, you'll see why I'm the trainer and not the cook.
[source] Single and double quoted strings, triple quoted strings, raw strings and other ways to get \ characters into a string.
[source] Setting up a 3 x 3 matrix (I would normally encapsulate this within a class - my example on day 1 was a simple list of lists)
[source] A program which prints out its own (embedded) documentation. Good programming practise is something I talk about on all the courses that I run, from the first day. However, writing code that is robust, reliable, easy to use and easy to maintain requires a wide range of the facilities that the language offers, so this example tells you that (for example) it is missing input validation.
Day 2

[source] An example that uses a generator to provide just-in-time values from a data source, rather than making up a whole (potentially huge) list first from which they are processed one by one. With a generator, care needs to be taken to ensure that you don't simply end up producing a list within the generator function, and you'll note that I've used xrange rather than range. When we get to Python 3, that will change as range will itself be a generator in this circumstance.
[source] An example of the scoping of variables in function calls, and also of optional parameters. Although you'll want most variables to be local, Python does offer the ability to have globals, and variables within the object and name space of the method too. And read-only references to variables that are unset in a function also cause it to look at the outer scope, which can be convenient if used with care. This example shows an awful lot of things that shouldn't be used a great deal ...
[source] A tiny module! ... and [source] a tiny program that imports code from a module.
[source] This example shows how a list can be passed into a function and have its contents altered within the function. Actually, this is the default behaviour of mutable parameters in Python ...
[source] and [source]. An important demo that shows how a data file can be read, the lines analysed and used to build up a list of objects. The objects are then compared (I wrote methods like elder and eldest) and various analyses are produced. A relly useful step towards a real life, practical program with most of the elements coming together.
[source] How to handle data files that aren't clean - this example uses regular expressions to process a data file which has a mixture of tabs and spaces (and sometimes multiple spaces) between fields.
[source] The example I described a few article ago that translates OSI (Ordnance Survey of Ireland) grid references into East and North measurements across the country. Great example of regular expressions, and it allowed me to show various other things like the command line handler and string to integer conversions.
Day 3

[source] A class of cubic containers, which can be added together to make a larger container (larger x, larger y, sum of the zs). And a test program that works out how big a bag you need if you put a fillet of fish on top of a big mac!
[source] Some time functions!
[source] A GUI example using wxPython (wx was THE GUI that this group wanted!) ... this example being a short but complete example.
And I'll finish with [source] of a team effort - written by delegates, with some of my assistance - to build a complete GUI that let them select the "from" and "to" of a journey. It contains examples of each of the major elements - various widgets / components, placement of them, events, and the use of the event handlers to create and manipulate objects in the 'main' Python code.
Summary

If you would like me to help you learn Python too, I would be delighted! There's a link to our public course description - with the next dates - here. If you have a group of delegates, we can run a private (tailored) course for you - see here for a course run at our training centre in Melksham, Wiltshire (should you live too far away to commute, you're in good company with many of our delegates - which is why we're also a hotel). And finally, if you've got a group of half a dozen or more, I can come to you and train on site. Let me paste in a couple of links to quotations ... for a Python course in Edinburgh, for Classes in Python in Plymouth and if you would like me to teach Python in Georgia (that's the USA state of Georgia - please email me for the country of Georgia!)
The Images are local Melksham pictures - all taken within about 1km of our training centre.
At Well House Manor - from 12th May 2012 - The Well House Collection - a branch of the Museum of Melksham.