How do I calculate a mean with python?

Jugurtha Hadjar jugurtha.hadjar at gmail.com
Mon Sep 16 20:08:42 EDT 2013


On 09/17/2013 12:33 AM, William Bryant wrote:
> Hey I am new to python so go easy, but I wanted to know how to make a program that calculates the maen.
>
> List = [15, 6, 6, 7, 8, 9, 40]
> def mean():
>      global themean, thesum
>      for i in List:
>          thecount = List.count(i)
>          thesum = sum(List)
>      themean = thesum / thecount
>
> Why doesn't this work?
>

You want to compute the arithmetic mean, I suppose .. Which is the sum 
of all, divided by the number of samples. i.e: If you have n numbers or 
elements, your mean (arithmetic) would be mean = sum(elements)/n. Right ?

And then, n also doesn't need to be in the loop and list.count(i) gives 
the occurrences of i in your list, which you don't need.

list.count(15) gives 1.
thesum/list.count(15) doesn't change.


-- 
~Jugurtha Hadjar,



More information about the Python-list mailing list