Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # using logic
- import math
- def get_input():
- size = int(input("Enter length of array: "))
- num_data = []
- for i in range(size):
- number = int(input("Enter element {}: ".format(i+1)))
- num_data.append(number)
- return num_data
- def mean(data):
- return sum(data) / len(data)
- def variance(data):
- mu = mean(data)
- return sum((x - mu) ** 2 for x in data) / len(data)
- def std_deviation(data):
- return math.sqrt(variance(data))
- data = get_input()
- print("Mean:", mean(data))
- print("Variance:", variance(data))
- print("Standard Deviation:", std_deviation(data))
- #using library
- import numpy as np
- def get_input():
- size = int(input("Enter length of array: "))
- num_data = []
- for i in range(size):
- number = int(input("Enter element {}: ".format(i+1)))
- num_data.append(number)
- return num_data
- def mean(data):
- return np.mean(data)
- def variance(data):
- return np.var(data)
- def std_deviation(data):
- return np.std(data)
- data = get_input()
- print("Mean:", mean(data))
- print("Variance:", variance(data))
- print("Standard Deviation:", std_deviation(data))
- #Output
- Enter length of array: 12
- Enter element 1: 103
- Enter element 2: 85
- Enter element 3: 117
- Enter element 4: 98
- Enter element 5: 203
- Enter element 6: 119
- Enter element 7: 48
- Enter element 8: 65
- Enter element 9: 86
- Enter element 10: 110
- Enter element 11: 180
- Enter element 12: 156
- Mean: 114.16666666666667
- Variance: 1904.138888888889
- Standard Deviation: 43.63643991996699
Advertisement
Advertisement