✐

✐

“46029˙CH04˙Miller” — 2008/8/11 — 6:58 — page 131 — #15

✐

✐

✐

✐

✐

✐

4.5 Central Tendency 131

1 def getMax(alist):

2 maxSoFar = alist[0]

3 for item in alist[1:]:

4 if item > maxSoFar:

5 maxSoFar = item

6

7 return maxSoFar

Listing 4.3 Alternative function to return the maximum value in a list

Exercises

4.14 Implement the getMin function using iteration by index.

4.15 Implement the getMin function using iteration by item.

4.16 Rewrite the getRange function using getMin and getMax.

4.5 Central Tendency

One of the most often used measures of a collection of data is known as central tendency—

a process that estimates where the “center” of a collection will be found. There are three

common ways to compute central tendency: (1) mean, (2) median, and (3) mode.

4.5.1 Mean

The most common measure of central tendency is the mean also called the average.To

compute the mean, we simply add up the values in a collection and divide by the number

of items. Listing 4.4 shows how this is done. Given a list of values, we use the sum function

to compute a total and the len function to compute the number of values. The mean is

simply the sum divided by the length. Session 4.10 shows the mean function in action.

1 def mean(alist):

2 mean = sum(alist) / len(alist)

3 return mean

Listing 4.4 Computing the mean of a list

Get *Python Programming in Context* now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.