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 ...
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!
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.
Single and double quoted strings, triple quoted strings, raw strings and other ways to get \ characters into a string.
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)
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.
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!
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.
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 ...
A tiny module! ... and [source]
a tiny program that imports code from a module.
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 ...
. 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.
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.
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.
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!
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!
Some time functions!
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.
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 articlesY050 - Python - General 
Identifying the first and last records in a sequence - (2016-02-26) 
Well House Consultants - Python courses / what's special. - (2015-10-28) 
Public training courses - upcoming dates - (2015-02-21) 
Additional Python courses added to our schedule - (2015-01-29) 
A longer Python ... training course - (2014-09-16) 
Using Python to analyse last years forum logs. Good coding practise discussion. - (2014-01-01) 
Whether you have programmed before or not, we can teach you Python - (2012-11-25) 
How well do you know Perl and / or Python? - (2012-11-04) 
Python Programming class for delegates who have already self-taught the basics - (2012-10-25) 
Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22) 
Want to escape the Olympics? Learn to program in the countryside! - (2012-07-23) 
When you should use Object Orientation even in a short program - Python example - (2012-07-06) 
Python - current versions and implementations (CPython, Jython, IronPython etc) - (2011-11-13) 
Python courses and Private courses - gently updating our product to keep it ahead of the game - (2011-10-20) 
Busy weekend of contrasts. - (2011-10-03) 
Python through the Snow - (2010-12-01) 
Python training courses for use with ESRI ArcMap software - (2010-06-23) 
Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24) 
Learning to program in ... - (2009-11-15) 
Two days of demonstration scripts in Python - (2009-09-05) 
Learning to program - how to jump the first hurdles - (2009-08-20) 
Great new diagrams for our notes ... Python releases - (2009-07-13) 
Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11) 
Python - a truly dynamic language - (2009-01-30) 
Python training - (2004-08-16)Y117 - Python - Already written modules 
Binary data handling - Python and Perl - (2015-03-09) 
Reading command line parameters in Python - (2015-02-23) 
Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14) 
JSON from Python - first principles, easy example - (2013-05-13) 
Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14) 
How can I do an FTP transfer in Python? - (2011-10-05) 
A demonstration of how many Python facilities work together - (2011-09-16) 
Syncronise - software, trains, and buses. Please! - (2010-08-22) 
Dates and times in Python - (2010-07-27) 
Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
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