Home > Backend Development > Python Tutorial > How to calculate basic statistical values ​​in python?

How to calculate basic statistical values ​​in python?

coldplay.xixi
Release: 2020-06-20 15:32:58
Original
5215 people have browsed it

How to calculate basic statistical values ​​in python?

How to calculate basic statistics using python?

The code for calculating basic statistical values ​​​​in python is

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;
    return pow(sdev/(len(numbers)-1), 0.5);
 
def median(numbers):#计算中位数
    sorted(numbers);
    size = len(numbers);
    if size%2 == 0:
        med = (numbers[size//2-1] + numbers[size//2])/2;
    else:
        med = numbers[size//2];
    return med;
 
n = getNum();
m = mean(n);
 
print("平均值:{},方差:{:.2f},中位数:{}.".format(m, dev(n,m),median(n)));
 
#运行结果
#请输入数字(回车退出):10
#请输入数字(回车退出):20
#请输入数字(回车退出):30
#请输入数字(回车退出):
#平均值:20.0,方差:10.00,中位数:20.
Copy after login

Recommended tutorial: "python video tutorial"

The above is the detailed content of How to calculate basic statistical values ​​in python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template