I 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] An answer to the practical exercise in which I ask delegates to come up with a list of 31 1-s, 28 2-s, 31 3-s, etc so that they can look up day number (say) 166 and see which month number it is in. The sample answer uses the power of Python's list handlers and does NOT involve nested loops!
[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] In Python, you can define then redefine a function, and you can even put alternative function definitions within an "if" and its "else". Occasionally, you might even
want to do this!
[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] Reading an input from the user, and converting it into an integer. If the user gets the format wrong (types in something silly!) it will flag an error and reprompt. Good use of exceptions within a loop all within a function!
[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
That's a long post to show just how many extra examples I write during a typical course - though it
is unusual for me to document most of them in this way (even on this course, there are a few intermediate files that I'm not posting). If you view any of the source codes, you'll see links to other similar examples too on the right of the page.
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.
(written 2009-01-31, updated 2012-03-08)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y117 - Python - Already written modules [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
[2890] Dates and times in Python - (2010-07-27)
[2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
[3442] A demonstration of how many Python facilities work together - (2011-09-16)
[3465] How can I do an FTP transfer in Python? - (2011-10-05)
[3479] Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14)
[4085] JSON from Python - first principles, easy example - (2013-05-13)
[4086] Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
[4441] Reading command line parameters in Python - (2015-02-23)
[4452] Binary data handling - Python and Perl - (2015-03-09)
[4696] Programming with random numbers - yet re-using the same values for testing - (2016-06-22)
[4697] Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23)
[4708] Scons - a build system in Python - building hello world - (2016-10-29)
[4710] Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)
Y050 - Python - General [16] Python training - (2004-08-16)
[2017] Python - a truly dynamic language - (2009-01-30)
[2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
[2285] Great new diagrams for our notes ... Python releases - (2009-07-13)
[2367] Learning to program - how to jump the first hurdles - (2009-08-20)
[2394] Two days of demonstration scripts in Python - (2009-09-05)
[2504] Learning to program in ... - (2009-11-15)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[2822] Python training courses for use with ESRI ArcMap software - (2010-06-23)
[3076] Python through the Snow - (2010-12-01)
[3463] Busy weekend of contrasts. - (2011-10-03)
[3489] Python courses and Private courses - gently updating our product to keep it ahead of the game - (2011-10-20)
[3519] Python - current versions and implementations (CPython, Jython, IronPython etc) - (2011-11-13)
[3798] When you should use Object Orientation even in a short program - Python example - (2012-07-06)
[3816] Want to escape the Olympics? Learn to program in the countryside! - (2012-07-23)
[3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
[3903] Python Programming class for delegates who have already self-taught the basics - (2012-10-25)
[3911] How well do you know Perl and / or Python? - (2012-11-04)
[3935] Whether you have programmed before or not, we can teach you Python - (2012-11-25)
[4236] Using Python to analyse last years forum logs. Good coding practise discussion. - (2014-01-01)
[4295] A longer Python ... training course - (2014-09-16)
[4408] Additional Python courses added to our schedule - (2015-01-29)
[4434] Public training courses - upcoming dates - (2015-02-21)
[4558] Well House Consultants - Python courses / what's special. - (2015-10-28)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
Some other Articles
Carry on Training - in spite of the weathersw_vers - what version of OSX am I running?Pre and post increment - the ++ operatorShort health and safety courseLearning Python - many new example programsBaby Caleb and Fortune City in your web logs?UnboundLocalError - Python MessageApache httpd and Apache Tomcat miscellanyService Excellence Awards