Logo Les Interros des Lycées
08-01.py
Programme Python de l'Interros des Lycées 1re M
X = [-5,-3,0,1,2,10]
P = [0.2,0.3,0.05,0.05,0.3,0.1]

def esperance(X,P):
    e = 0
    for i in range(len(X)):
        e += X[i]*P[i]
    return e

def variance(X,P):
    v = 0
    e = esperance(X,P)
    for i in range(len(X)):
        v += P[i]*(X[i]-e)**2
    return v

def ecartype(X,P):
    return variance(X,P)**0.5

print("L'espérance est :",esperance(X,P))
print("La variance est :",variance(X,P))
print("L'écart-type est :",ecartype(X,P))

Y = []
for i in X:
    Y.append(i*i)

V = esperance(Y,P) - esperance(X,P)**2
print(V)