I'm fairly new to python and I've currently run into a road block in this problem. I set up this code:

def average(the_list):
    return the average of the list
def deltalist(the_list,a):
    return a list which is each of the element of the_list subtracted by a
def squarelist(lst):
    return a list which is each of the element of the_list squared
def variance(the_list,mean):
    return squarelist(deltalist(the_list,mean))
def stddev(l):
    a = average(l)
    l3 = variance(l,a)
    return math.sqrt(sum(l3)/(len(l3) - 1))
lst = [1,3,4,6,9,19]

the return statements is the problem I've ran into. I want to implement the average, squarelist, and the delta list to find the standard deviation of the list that I've used in this code. How do I use the numbers that I have listed in lst to get standard deviation?

5 Years
Discussion Span
Last Post by sick vapor

Membership rules

Keep It Organized

  • Do provide evidence of having done some work yourself if posting questions from school or work assignments

This is my work as the only thing that was provided was the numbers that I listed, but I will try to complete the code to the best of my knowledge as I don't yet get the full understanding of the return statements and arguments that follow. Thanks for the response.

Edited by sick vapor: wanted to say thanks


This is tougher, if you do not understand fully list comprehensions, but I recommend to learn them. Here is list comprehension version

def squarelist(the_list):
    return [element**2 for element in the_list] # a list with element squared for each of the element in the_list

Edited by pyTony


This is what I compiled:

import math

q = [1,3,4,6,9,19]
avg = float(sum(q))/len(q)
dev = []
for x in q:
    dev.append(x - avg)
sqr = []
for x in dev:
    sqr.append(x * x)
mean = sum(sqr)/len(sqr)
standard_dev = math.sqrt(sum(sqr)/(len(sqr)-1))
print("the standard deviation of set %s is %f" % (q, standard_dev) )

thanks for your guys help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.