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] (written 2010-12-11, updated 2010-12-13)
2fce
Associated topics are indexed under
Y102 - Python - Fundamentals [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[1878] Pascals Triangle in Python and Java - (2008-11-10)
[1461] Python - input v raw input - (2007-12-06)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1430] Integer v float - Python - (2007-11-12)
[956] Python security - trouble with input - (2006-11-30)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[633] Copying a reference, or cloning - (2006-03-05)
[328] Making programs easy for any user to start - (2005-05-29)
Y103 - Python - Conditionals and Loops [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[3762] Learning to program - the if statement. Python. - (2012-06-12)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1661] Equality, sameness and identity - Python - (2008-05-31)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1201] No switch in Python - (2007-05-23)
[909] Python is like a narrowboat - (2006-10-30)
[835] Python - when to use the in operator - (2006-08-16)
[788] New - Conditional expressions in Python 2.5 - (2006-07-01)
[668] Python - block insets help with documentation - (2006-04-04)
[657] The ternary operator in Python - (2006-03-25)
[353] Wimbledon Neck - (2005-06-20)
[299] What - no switch or case statement? - (2005-05-03)
Y110 - Python - File Handling [3764] Shell, Awk, Perl of Python? - (2012-06-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)
[2870] Old prices - what would the equivalent price have been in 1966? - (2010-07-14)
[2282] Checking robots.txt from Python - (2009-07-12)
[2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
[1442] Reading a file multiple times - file pointers - (2007-11-23)
[183] The elegance of Python - (2005-01-19)
[114] Relative or absolute milkman - (2004-11-10)
Some other Articles
Making the most of critical emails - reading behind the sceneSizers (geometry control) in a wxPython GUI - a first exampleObject Oriented Programming for Structured Programmers - conversion trainingCan you trust the big brand names? Python - fresh examples from recent coursesXML handling in Python - SAX, DOM and XSLT exampleswxPython - simple example to add GUI to a server log file analysisSanta Special - rather more special than usual - December 2010TransWilts Rail News ... Melksham (Santa Special) Edition ... 5th December 2010Royal Wedding. How William and Catherine have changed our schedule