OKADA LABO

[Python]listを使って平均値をもとめる

listを使って平均値をもとめる

list = [10 ,20, 30, 40, 50]
sum = 0
for i in range(len(list)):
    sum = sum + list[i]
answer = sum / (len(list))
print(answer)

-> 30.0

NumPyの配列を使う場合

import numpy as np
narray = np.array([10 ,20, 30, 40, 50])
answer = np.mean(narray)
print(answer)

-> 30.0