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?

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.