基本统计值:
总个数: len()
求和: for ... in
平均值: 求和/总个数
方差: 各数据与平均数差的平方的和的平均数
中位数: 排序,然后... 奇数找中间1个,偶数找中间2个取平均
def getNum():
nums = []
iNumStr = input("请输入数字(回车退出):")
while iNumStr != "":
nums.append(eval(iNumStr))
iNumStr = input("请输入数字(回车退出):")
return nums
def mean(numbers):
s = 0.0
for num in numbers:
s = s + num
return s / len(numbers)
def dev(numbers, mean):
sdev = 0.0
for num in numbers:
sdev = sdev + (num - mean) **2
def median(numbers):
sorted(numbers)
size = len(numbers)
if size % 2 == 0:
med = (numbers[size//2 -1] + number[size//2]) / 2
else:
med = numbers[size//2]
return med
n = getNum()
m = mean(n)
print("平均值:{},方差:{:.2},中位数:{}".format(m, dev(n,m), median(n)))
https://www.jianshu.com/p/794a61dbfd4c