-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exc 62.py
65 lines (45 loc) · 1.49 KB
/
Exc 62.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# calculate the standart deviation of 8,9,10 and print the result
x1, x2, x3 = 10, 11, 9
mean = (x1 + x2 + x3) / 3.0
var = ((x1 - mean) ** 2 + (x2 - mean) ** 2 + (x3 - mean) ** 2) / 3.0
std = var ** (1 / 2)
print(f"The standard deviation: {std:.2f}")
# or
import math
# Given numbers
numbers = [8, 9, 10]
# Step 1: Calculate the mean
mean = sum(numbers) / len(numbers)
# Step 2: Calculate the deviations from the mean
deviations = [(x - mean) for x in numbers]
# Step 3: Square each deviation
squared_deviations = [d**2 for d in deviations]
# Step 4: Calculate the variance
variance = sum(squared_deviations) / len(numbers)
# Step 5: Calculate the standard deviation
standard_deviation = math.sqrt(variance)
print(standard_deviation)
# or
# Given numbers
numbers = [8, 9, 10]
# Step 1: Calculate the mean
mean = sum(numbers) / len(numbers)
# Step 2: Calculate the deviations from the mean
deviations = []
for x in numbers:
deviations.append(x - mean)
# Step 3: Square each deviation
squared_deviations = []
for d in deviations:
squared_deviations.append(d**2)
# Step 4: Calculate the variance
variance = sum(squared_deviations) / len(numbers)
# Step 5: Calculate the standard deviation (square root of the variance)
# Implementing the square root function manually
def sqrt(number, epsilon=1e-10):
guess = number / 2.0
while abs(guess * guess - number) > epsilon:
guess = (guess + number / guess) / 2.0
return guess
standard_deviation = sqrt(variance)
print(standard_deviation)