Training, Open Source Programming Languages

This is page ... grams.html

Our email: • Phone: 01144 1225 708225

Learning Python - many new example programs

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

Glowing Tree[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

Loops on the River Avon[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

Where the Wilts and Berks canal used to pass under the Melksham to Devizes Road[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.


Formerly Historic Society museum, now Love LooLoo, round houseThat'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 for individual articles
Y050 - Python - General
  [4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
  [4656] Identifying the first and last records in a sequence - (2016-02-26)
  [4558] Well House Consultants - Python courses / what's special. - (2015-10-28)
  [4434] Public training courses - upcoming dates - (2015-02-21)
  [4408] Additional Python courses added to our schedule - (2015-01-29)
  [4295] A longer Python ... training course - (2014-09-16)
  [4236] Using Python to analyse last years forum logs. Good coding practise discussion. - (2014-01-01)
  [3935] Whether you have programmed before or not, we can teach you Python - (2012-11-25)
  [3911] How well do you know Perl and / or Python? - (2012-11-04)
  [3903] Python Programming class for delegates who have already self-taught the basics - (2012-10-25)
  [3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
  [3816] Want to escape the Olympics? Learn to program in the countryside! - (2012-07-23)
  [3798] When you should use Object Orientation even in a short program - Python example - (2012-07-06)
  [3519] Python - current versions and implementations (CPython, Jython, IronPython etc) - (2011-11-13)
  [3489] Python courses and Private courses - gently updating our product to keep it ahead of the game - (2011-10-20)
  [3463] Busy weekend of contrasts. - (2011-10-03)
  [3076] Python through the Snow - (2010-12-01)
  [2822] Python training courses for use with ESRI ArcMap software - (2010-06-23)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [2504] Learning to program in ... - (2009-11-15)
  [2394] Two days of demonstration scripts in Python - (2009-09-05)
  [2367] Learning to program - how to jump the first hurdles - (2009-08-20)
  [2285] Great new diagrams for our notes ... Python releases - (2009-07-13)
  [2227] Learning PHP, Ruby, Lua and Python - upcoming courses - (2009-06-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [16] Python training - (2004-08-16)

Y117 - Python - Already written modules
  [4710] Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)
  [4708] Scons - a build system in Python - building hello world - (2016-10-29)
  [4697] Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23)
  [4696] Programming with random numbers - yet re-using the same values for testing - (2016-06-22)
  [4452] Binary data handling - Python and Perl - (2015-03-09)
  [4441] Reading command line parameters in Python - (2015-02-23)
  [4086] Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
  [4085] JSON from Python - first principles, easy example - (2013-05-13)
  [3479] Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14)
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2890] Dates and times in Python - (2010-07-27)
  [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)

Back to
Baby Caleb and Fortune City in your web logs?
Previous and next
Horse's mouth home
Forward to
Short health and safety course
Some other Articles
Carry on Training - in spite of the weather
sw_vers - what version of OSX am I running?
Pre and post increment - the ++ operator
Short health and safety course
Learning Python - many new example programs
Baby Caleb and Fortune City in your web logs?
UnboundLocalError - Python Message
Apache httpd and Apache Tomcat miscellany
Service Excellence Awards
4724 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page

This is a page archived from The Horse's Mouth at - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2017: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho

PAGE: ... grams.html • PAGE BUILT: Sat Jun 11 12:16:26 2016 • BUILD SYSTEM: WomanWithCat