ThegetMedian () function finds the middlemost value once the sequence is sorted In the following getMedian () example, notice the use of the modulus operator (%), the if statement, the else clause, amd the built-in sort() operation.

def getMedian (nums):

"Find the Median number"

# create a duplicate since # we are going to modify it seq = nums[:]

#sort the list of numbers seq.sort()

median = None # to hold the median value length = len(seq) # to hold the length of the seq

# Check to see if the length is an even number if ( ( length % 2) == 0):

# since it is an even number

# add the two middle numbers together index = length / 2

# since it is an odd number

# just grab the middle number index = (length / 2)

median = seq[index] return median

Once again, let's break it down.

First we duplicate the nums sequence and sort the duplicate (seq).


Then, with the expression length%2, we check if the length is an even number. (Remember that the modulus operator returns the remainder.) If the length is even, the expression length%2 returns zero, and we calculate the median by adding together the two most central numbers and figuring their average.

length = len(seq)

median = (seq[index-1] + seq[index]) /2.0 If the length is odd, we grab the meddle value.

Finally we return the median.

return median reports tatistics()

reportStatistics () calls all of the functions implemented in our house prices example—getMean ( ) ,getMode ( ) ,getRange () ,getMedian (), and nested dictionaries—and stores their return values in two dictionaries, averages and ranges. It puts these dictionaries in another dictionary called report, which it returns.

def reportStatistics (nums):

# get central tendencies averages = {

"mean":getMean(nums,0), "median":getMedian(nums) ,

"mode":getMode(nums) }

# get range range = getRange(nums)

# put ranges in a dictionary ranges = {

"min":range[0], "max":range[1],

"averages": averages,

"ranges": ranges }

re turn report

Breaking this down, we first get the averages—mean, median, and mode—using getMean ( ), getMedian ( ), and getMode ( ). Notice that "mean" : getMedian defines a key/value pair.

"mean":getMean(nums ,0), "median":getMedian(nums) ,

Then we get the range parameters—min,max, and max-min—fromgetRange ( ). We use range [0] , range [1] , and range [2] in the returned se quence. Notice that "min" : range [ 0 ] defines a key/value pair in the ranges dictionary.

# get range range = getRange(nums )

# put ranges in a dictionary ranges = {

"min": range [0], "max": range [1],

Now we define a dictionary called report that contains the averages and ranges dictionaries.

" averages": aver ages,

"ranges": ranges }

Lastly we return the report dictionary.

return report

Using reportStatistics()

RunReport() uses reportStatistics () to get the report dictionary it needs to print out the report. In the following runReport ( ) e xample, note the use of the string format operator (%), the %f format directive, nested dictionaries, and the use of the format operator with a dictionary.

from chap4 import reportStatistics house_in_awahtukee = [100000, 120000, 150000, 200000, 65000, 100000] report = reportStatistics(house_in_awahtukee)

The least expensive house is %(min)20.2f The most expensive house is %(max)20.2f The range of house price is %(range)20.2f average_format = """ Averages:

The mean house price is %(mean)20.2f The mode for house price is %(mode)20.2f The median house price is %(median)20.2f print range_format % report["ranges"]

print average_format % report["averages"| Here's the output: Range:

The least expensive house is 65000.00 The most expensive house is 200000.00 The range of house price is 135000.00


The mean house price is 122500.00

The mode for house price is 100000.00 ge fo

The median house price is 110000.00

Was this article helpful?

0 0

Post a comment