182 CHAPTER11. DATA COLLECTIONS
for num in nums:
sum = sum + num
return sum / len(nums)
Noticehow theaverageis computedandreturnedinthelastlineofthisfunction.Thelenoperationreturns
thelengthofa list;wedon’t needa separateloopaccumulatortodeterminehow many numbersthereare.
Withthesetwo functions,ouroriginalprogramtoaveragea seriesofnumberscannowbedoneintwo
simplelines:
def main():
data = getNumbers()
print ’The meanis’, mean(data)
Next,let’s tacklethestandarddeviationfunction,stdDev. Inorderusethestandarddeviationformula
discussedabove, wefirstneedtocomputethemean.We have a designchoicehere.Thevalueofthemean
caneitherbecalculatedinsideofstdDevorpassedtothefunctionasa parameter. Whichwayshouldwe
doit?
Ontheonehand,calculatingthemeaninsideofstdDevseemscleaner, asit makestheinterfacetothe
functionsimpler. To getthestandarddeviationofa setofnumbers,wejustcallstdDevandpassit the
listofnumbers.Thisis exactlyanalogoustohowmean(andmedianbelow)works.Ontheotherhand,
programsthatneedto computethestandarddeviationwillalmostcertainlyneedto computethemeanaswell.
Computingit againinsideofstdDevresultsinthecalculationsbeingdonetwice.If ourdatasetis large,
thisseemsinefficient.
Sinceourprogramisgoingtooutputboththemeanandthestandarddeviation,let’s have themain
programcomputethemeanandpassit asa parametertostdDev. Otheroptionsareexploredin theexercises
at theendofthechapter.
Hereis thecodetocomputethestandarddeviationusingthemean(xbar) asa parameter:
def stdDev(nums, xbar):
sumDevSq = 0.0
for num in nums:
dev = xbar- num
sumDevSq = sumDevSq+ dev * dev
return sqrt(sumDevSq/(len(nums)-1))
Noticehow thesummationfromthestandarddeviationformulais computedusinga loopwithanaccumulator.
ThevariablesumDevSqstorestherunningsumofthesquaresofthedeviations.Oncethissumhasbeen
computed,thelastlineofthefunctioncalculatestherestoftheformula.
Finally, wecometothemedianfunction.Thisoneis a littlebittrickier, aswedonothave a formulato
calculatethemedian.We needanalgorithmthatpicksoutthemiddlevalue.Thefirststepis toarrangethe
numbersinincreasingorder. Whatevervalueendsupinthemiddleofthepackis,bydefinition,themedian.
Thereis justonesmallcomplication.If wehave anevennumberofvalues,thereis noexactmiddlenumber.
In thatcase,themedianis determinedbyaveragingthetwo middlevalues.Sothemedianof3,5,6 and9 is
5 6
2 5 5.
Inpseudocodeourmedianalgorithmlookslike this.
sort the numbers intoascending order
if the size of datais odd:
median = the middlevalue
else:
median = the averageof the two middle values
return median
ThisalgorithmtranslatesalmostdirectlyintoPythoncode.We cantake advantageofthesortmethodto
putthelistinorder. To testwhetherthesizeis even,weneedtoseeif it is divisiblebytwo.Thisis a perfect
applicationoftheremainderoperation.Thesizeis evenifsize % 2 == 0, thatis,dividingby2 leavesa
remainderof0.
Withtheseinsights,wearereadytowritethecode.