# Statistics with Lists¶

In [15]:
from math import sqrt
from random import sample

In [16]:
def get_numbers_rand():
return sample(range(1, 1000), k=300)

In [4]:
def get_numbers():
# Acumulator variable, just as a list
nums = []

x_str = input("Please enter a number (<Enter to quit) >> ")
while x_str != "":
x = float(x_str)
nums.append(x)
x_str = input("Please enter a number (<Enter to quit) >> ")

return nums

In [5]:
def mean(nums):
_sum = 0.0
for num in nums:
_sum += num

# Get the average and return
return _sum/len(nums)

In [6]:
def std_dev(nums, xbar):
sum_dev_sq = 0.0
for num in nums:
dev = xbar - num
sum_dev_sq += dev**2
return sqrt(sum_dev_sq/(len(nums) - 1))


### Median¶

In [7]:
even = [1,2,3,4,5,6]
print(len(even)/2)
print(len(even)//2)

3.0
3

In [8]:
odd = [1,2,3,4,5,6, 7]
print(len(odd)/2)
print(len(odd)//2)

3.5
3

In [9]:
def median(nums):
nums.sort()
size = len(nums)
mid_pos = size//2
if size % 2 == 0:
# List 'nums' is even-lengthed
median = (nums[mid_pos] + nums[mid_pos - 1])/2
else:
# List 'nums' is odd-lengthed
median = nums[mid_pos]

return median

In [18]:
def main():
#data = get_numbers()
data = get_numbers_rand()
xbar = mean(data)
std = std_dev(data, xbar)
med = median(data)
print("\nThe mean is {0:0.2f}".format(xbar))
print("The standard deviation is {0:0.2f}".format(std))
print("The median is {0:0.2f}".format(med))

In [19]:
main()

The mean is 517.71
The standard deviation is 289.34
The median is 530.00

In [ ]: