Functions

Function definitions can happen anywhere in the program or in a separate file. For example, the above average calculations can be defined as:

def myaverage(y):
    s = 0.0
    for v in y:
        s = s + v
    return (s/len(y))
and then called from the main program as myaverage(a). The keyword “def” indicates the start of the function definition. Note again the 4 character indent for the function body and the colon after the function name. This is characteristic for python.



github / contact

Creative Commons License