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 course
Python Programming
Examples from our training material
| 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 |
| fat.py | A second program that uses shared functions in mod2.py |
| flook.py | Program to use a module and the attributes of its functions |
| fun.py | definition and use of a function |
| gen2 | Using a generator function to provide an iteration |
| genex | Generator v regular function |
| local.py | File of functions to be loaded by other examples |
| locvar.py | Local and global variables |
| 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 |
| mystuff.py | functions, statics, test code, doc strings ... |
| params.py | Optional parameters, and variable number of parameters |
| passenger | Calling program - sample answer with separate module for functions |
| 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 |
| taxi | Exercise answer - net and tax from gross amount |
Opentalk forum discussions related to this topic
Should I use from or importVariable scope in PythonCommon Python questions
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
Why use functions.
A first function.
A first module.
More flexibility in calling functions.
Doc strings and function attributes.
Defining your own attributes.
Variable scope.
Advanced function capabilities and recent additions.
Lambda example.
Generator Example.
Recursion.
Built-in 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 600 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.