EXPERIMENT No. 10
Python Lab Practical Record · Python programming laboratory
Read with diagrams, search and self-test
10A. Mean, variance and standard deviation
Aim
To calculate the mean, variance and standard deviation of data using Python.
Formula / Note
statistics.variance() and statistics.stdev() give the sample variance and standard deviation (divisor n − 1).
Program (mean_variance_sd.py)
# Exp 10A: Mean, variance and standard deviation import statistics
data = [10, 12, 14, 16, 18]
mean = statistics.mean(data)
variance = statistics.variance(data)
std = statistics.stdev(data)
print("Mean =", mean)
print("Variance =", variance)
print("Standard Deviation =", round(std, 2))Output
Mean = 14 Variance = 10 Standard Deviation = 3.16
Result
Mean = 14, variance = 10 and standard deviation = 3.16 were obtained for the given data.
10B. X-bar and R control charts from sample data
Aim
To generate X-bar and R control charts from sample data using matplotlib.
Program (xbar_r_control_chart.py)
# Exp 10B: X-bar and R control charts from sample data import matplotlib.pyplot as plt
xbar = [11, 12, 13, 12, 11.25] R = [2, 2, 2, 2, 2] sample_no = [1, 2, 3, 4, 5]
plt.plot(sample_no, xbar, marker='o')
plt.title("X-bar Chart")
plt.xlabel("Sample number")
plt.ylabel("Average")
plt.show()plt.plot(sample_no, R, marker='o')
plt.title("R Chart")
plt.xlabel("Sample number")
plt.ylabel("Range")
plt.show()Output
(The two graphs below are displayed by plt.show())
X-bar chart
R chart
Result
The X-bar and R control charts were generated from the sample data using matplotlib.