>>> # 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) >>> |