Quick question.....

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Wed Jan 14 16:07:15 EST 2004


Narsil wrote:
> I have a list, which consists of just numbers.
> 
> Is there a command that will allow me to total up these numbers and get an average?
> 
> Say for example, the list looks thus:
> 
> numberlist = [10, 2, 5, 7, 3, 46, 4, 5, 87, 5]
> 
> How would I get the total, and how would I get the average?

You could have guessed?

 >>> numberlist=[10, 2, 5, 7, 3, 46, 4, 5, 87, 5]
 >>> print sum(numberlist)
174
 >>> print sum(numberlist)/len(numberlist)          # watch out, integer result
17
 >>> print float(sum(numberlist))/len(numberlist)   # force float result
17.4


--Irmen



More information about the Python-list mailing list