22.2k views
3 votes
Write a class called Person that has two data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members.Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects (only one parameter: person_list) and returns the standard deviation of all their ages (the population standard deviation that uses a denominator of N, not the sample standard deviation, which uses a different denominator).

1 Answer

4 votes

Answer:

class Person(object):

def __init__(self, name, age):

self.name = name

self.age = age

def std_dev(person_list):

average = 0

for person in person_list:

average += person.age

average /= len(person_list)

total = 0

for person in person_list:

total += (person.age - average) ** 2

return (total / (len(person_list) )) ** 0.5

Step-by-step explanation:

The class "Person" is a python program class defined to hold data of a person (name and age). The std_dev function accepts a list of Person class instances as an argument and returns the calculated standard deviation of the population.

User Jachdich
by
7.1k points