Ask Question
2 October, 20:07

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 basic_stats that takes as a parameter a list of Person objects and returns a tuple containing the mean, median, and mode of all the ages. To do this, import the statistics library and use its built-in mean, median and mode functions. Your basic_stats function should return those three values as a tuple, in the order given above. For example, it could be used as follows: p1 = Person ("Kyoungmin", 73) p2 = Person ("Mercedes", 24) p3 = Person ("Avanika", 48) p4 = Person ("Marta", 24) person_list = [p1, p2, p3, p4] print (basic_stats (person_list)) # should print a tuple of three values The files must be named: std_dev. py

+1
Answers (1)
  1. 2 October, 21:33
    0
    The python std_dev. py file is given below. Every important statement is explained using comments.

    Explanation:

    import statistics

    class Person:

    """

    Contains two data members - the person's name and age.

    """

    def __init__ (self, name, age):

    """

    This init method takes two values - name and age - and uses them

    to initialize the data members.

    """

    self. name = name

    self. age = age

    def basic_stats (person_list):

    """

    This function takes as a parameter a list of Person objects and

    returns a tuple containing the mean, median, and mode of all the

    ages.

    """

    #list to store ages from person_list

    age = []

    #for loop for get age from each object of class Person

    for p in person_list:

    #appending age into list age from each object of class Person

    age. append (p. age)

    #calculating mean of ages

    age_mean = statistics. mean (age)

    #calculating median of ages

    age_median = statistics. median (age)

    #calculating mode of ages

    age_mode = statistics. mode (age)

    #returns mean, median, mode of ages

    #when we returns multiple values together it will return as tuple

    return age_mean, age_median, age_mode

    #objects of class Person

    p1 = Person ("Kyoungmin", 73)

    p2 = Person ("Mercedes", 24)

    p3 = Person ("Avanika", 48)

    p4 = Person ("Marta", 24)

    #list of objects

    person_list = [p1, p2, p3, p4]

    #calling function basic_stats and prints a tuple

    print (basic_stats (person_list)) # should print a tuple of three values
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “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 ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers