To execute the following code in Python, which module need to be imported ?…

2023

To execute the following code in Python, which module need to be imported ? print(__________.mean([1, 2, 3]))

  1. A.

    math / math

  2. B.

    random / random

  3. C.

    statistics / statistics

  4. D.

    probability / probability

Attempted by 1238 students.

Show answer & explanation

Correct answer: C

Answer: Import the statistics module and call statistics.mean on the list.

Example:

import statistics print(statistics.mean([1, 2, 3])) # Output: 2

Key points:

  • The statistics module provides a mean function that returns the arithmetic mean of numeric data.

  • The math and random modules do not have a mean function for lists.

  • If you do not want to import statistics, you can compute the mean manually with sum(data)/len(data), or use numpy.mean for arrays if numpy is available.

Explore the full course: Rssb Senior Computer Instructor