Main Content

Python - fresh examples from recent courses

Archive - Originally posted on "The Horse's Mouth" - 2010-12-11 08:09:58 - Graham Ellis

During courses, I write a lot of examples in front of delegates so that the get to see both a program to perform a certain task and how that solution was reached - after all, they'll be expected to modify and write programs after the course, and just showing them the final result would only be telling them a half of the story.

At the end of the course, these new programs are often useful for the delegates who attended, but not necessarily publishable to the wider world. They may lack comments, they may contain personal data, and they may have had 'side' demonstrations added into them which would make no sense to anyone who wasn't there. So I tidy up the examples, and add more comments too, before I public the useful ones. "Doesn't that take a long time?" someone asked me the other week. Yes - it does, but these new examples provide fresh alternative insights into how things are done, and build into a useful library.

I've been pretty full of cold / flu (?) for the last two weeks ... only firing on two cylinders rather than four ... so I've combined the extra examples from the Python course in Bristol (the week before last) and the public Python course from this week just concluded into a single post. All of the examples have additional descriptive comments - click on the [source] links to havea look.

Y102 - fundamentals

Printing out several values in the same print statement. You can provide a comma separated list of variables / expressions to a print statement, and Python will print them out one after another, with a space between them. If you want to suppress the space, you'll need to join the expressions together and the easiest way is often to connect them as strings using the "+" operator. [source]

Y103 - conditionals and loops

Conditionals in Python - conditions may be written without brackets, and the block of code that goes with the condition is inset rather than being surrounded by { to } or some other sort of delimiter. [source]

There's also an example (which I have placed in our "algorithms folder" - Q110 - which shows how we count the number of occurrences of a specific event within a loop. See [source]

Y105 - functions, modules and packages

A fresh demonstration of variable scope in Python - [source]

How xrange rather than range, and xreadlines rather than readlines, allow for efficient handling of large quantities of data without the need to store it all in memory - [source]

Calls to functions in another module (loaded from a separate file) - [source]

Y108 - string handling

Setting up Unicode text strings in Python - [source]

Raw and regular strings - [source]

Checking the type of data entered - [source]

Search and find, search and replace, and search and replace with the result of running code on what was found - [source]

Y109 - Exceptions

Exceptions to trap user errors - with a counter, so that if the user exceeds a certain number of erroneous inputs, the program exits - [source]

Reading from a remote URL ... with exception handling to trap the issue of the remote resource not being available [source]

Y110 - File Handling

Efficient programming to use huge amounts of data - example tests on 1Gbyte of text data records - [source]

Finding how much disc space is free from withing a Python program - using a wrapper to limit the OS dependent section to a set of functions isolated from the main code, which remains common. [source]

Sourcing data from a file using a "just in time" function so that no large intermediate list is produced [source]

Y111 - More on collections and sequences

Counting the number of times each visitor comes to a web site via a dictionary, then producing a sorted list. You're not able to sort a dictionary, but you can sort a list of the keys and then step through each of the keys in that sorted order. This example is also interesting in that it uses quite a complex sort function - it sorts firstly by the length of the IP address. If that doesn't tell records apart, it sorts by the number of visits from the IP address, and if there still isn't any distinction it sorts by the IP address itself. All of which is far longer to describe than to code - see [source].

Y112 - More OO Python

A "classic" demonstration - two slightly different types of people, with a lot of common (shared) code in a base class, and with differences defined in an extended class. A test program that uses that data, through Polymorphism to avoid the need for lots of conditional code, and also avoid the need for lots of repeated code - [source]. Sample data for that application is [here]

There's also a seasonal variation on the theme, with Christmas, Easter and Groundhog Day characters ... see [source]