<< Main Manual

Tutorial #0.5. Functions

TIP #1: The following examples are a crash course in Python programming showing the features that are most beneficial with the minimal amount of extraneous information. You can type the example code directly within PhysImage's 'Jython Console' or load the example files (*.py links below) directly from PhysImage as a demonstration, or test the code out with an independent installation of Python 2.7 on your preferred OS platform. These examples are independent of any specific PhysImage features and meant to be a succinct demonstration of very useful general Pythonic features.

TIP #2: The ">>> ", in the below example, is meant to represent PhysImage's 'Jython Console' prompt, while '#' in a line represents a comment (used to annotate Python code) and does not do anything. Therefore,
the text following the '#' is only for your benefit to understand the next bits of code and is not required to be typed in.

For a more expansive tutorial of the general Python programming language, see the Official Python Tutorial.

Example

The following code can be loaded within PhysImage from this file or typed in manually.

>>> # Defining Functions:
>>> #   Functions should be defined before they're used or imported from
>>> #   another file (more on this later)
>>>
>>> # defining a function that takes the mean of a list of values
>>> def mean(values):
...    meanValue = 0.0
...    for value in values:
...       # the following is equivalent to:
...       #     meanValue = meanValue + value
...       meanValue += value
...
...    # the following is equivalent to:
...    #     meanValue = meanValue/float(len(values))
...    meanValue /= float(len(values))
...    return meanValue
...
>>> # calculate the mean of a list of numbers from 0-99
>>> values = range(100)
>>> print values
>>>
>>> meanValue = mean(values)
>>> print "meanValue: " + str(meanValue)
>>>


After the data is in the WaveManager it got manipulated or plotted as a chart in the previously demonstrated ways.

<< Tutorial #0.4. Loops and If Statements
>> Tutorial #0.6. Files

<< Main Manual