Exercises, examples and other material relating to training module Y105. This topic is presented on public courses
Learning to program in Python,
Python Programming
Python Programming - The fundamentals in depth, and an introduction to applications such as GUI design and Data Munging.
This course starts at the fundamentals ... http://www.wellho.net/course/ypfull.html [course] |
#!/usr/local/bin/python
# Variable scope
first = 1
def one():
"Double a global variable, return it + 3."
global first
first ... http://www.wellho.net/resources/ex.php4?item=y105/locvar.py [code sample] |
Learning to Program in Python - The fundamentals in depth, and an introduction to applications such as GUI design and Data Munging.
This course starts ... http://www.wellho.net/course/ylfull.html [course] |
I'm not a great advocate of recursion - pieces of code that call themselves; most of the examples that I see in books are "training examples" that are ... http://www.wellho.net/mouth/912_Recu ... ython.html [short article] |
Function in PHP and Python and subs in Perl can only return one "thing" - one item, one object. But all three languages allow that returned item to be ... http://www.wellho.net/mouth/1202_Ret ... thon-.html [short article] |
When you write a program, you should split your code down into a series of named blocks, each of which performs a single logical action. Blocks will ... http://www.wellho.net/mouth/561_Pyth ... tions.html [short article] |
What's the difference between a function and a method? A function is a named piece of code that performs an operation, and a method is a function with ... http://www.wellho.net/mouth/900_Pyth ... ethod.html [short article] |
Python's "import" loads a Python module into its own namespace, so that you have to add the module name followed by a dot in front of references to any ... http://www.wellho.net/mouth/418_Diff ... ython.html [short article] |
In Python, blocks of code can be named. Such named blocks of code can be run from elsewhere in your program, and are known as functions. A module is a text file that contains a number of functions and other items, and a package is a group of related modules.
This topic is presented on public courses
Learning to program in Python,
Python Programming
Examples from our training material
| acsh2 | Dynamic function definition |
| cardy | Main program - methods and attributes from a module |
| colin.py | A tiny module |
| dfg | Using a generator to process a big file |
| dream | Good structure of named blocks of code |
| electric.py | a generator function - carries on where it left off at previous call |
| extrastep.py | importing a name from a module into the current namespace |
| f1 | more options with modules and functions |
| fat.py | A second program that uses shared functions in mod2.py |
| ffx | Dynamic function definition |
| flook.py | Program to use a module and the attributes of its functions |
| fun.py | definition and use of a function |
| gen | A generator - two code branches both live |
| gen2 | Using a generator function to provide an iteration |
| gen_control | No using a generator - each runs to completion |
| genex | Generator v regular function |
| gubbins.py | A bunch of functions in a module |
| local.py | File of functions to be loaded by other examples |
| locvar.py | Local and global variables |
| m1 | map to transform and filter to select from a list |
| mapfunc.py | Use of the map function to transform a list |
| mod2.py | A module with doc strings and attributes defined |
| mod_demo.py | Program that uses the module in local.py |
| mutton | Anonymous varaibles and subs and lambdas |
| myne.py | Module with documentation string and test harness |
| mystuff.py | functions, statics, test code, doc strings ... |
| noa | Mandatory, optional, ordered and named parameters |
| params.py | Optional parameters, and variable number of parameters |
| passenger | Calling program - sample answer with separate module for functions |
| poc.py | Scope of variables - some samples |
| qv2 | Example that uses mystuff.py |
| qv3 | alternative example using mystuff.py |
| recur.py | Recursion - when a function calls itself |
| sheep.py | A Lambda function - one line function definition |
| silksheets.py | Functions that we'll share between applications |
| slumbermore | Good structure of named blocks of code |
| snoresleep | A further Good structure of named blocks of code |
| sweetdream | Another example of a module reuse |
| sysdemo.py | use of a standard module (sys) |
| tax.py | functions to work out tax and net from gross |
| taxcalcs.py | Collecting function parameters |
| taxi | Exercise answer - net and tax from gross amount |
| yf | Using a generator |
| yum | Brining in elements from a namespace |
Pictures
Studious group during a practical
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Def and parameters. Default parameters.
return statments.
Variable scope and namespaces.
Global variables.
lambdas.
Doc strings.
Generator functions and yield.
from and import.
Built in and user defined modules.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.