176k views
1 vote
How to find mean in python without inbuilt function?

1 Answer

3 votes

Final answer:

To calculate the mean in Python without an inbuilt function, sum all numbers in the list and then divide by the count of the numbers. Use a for loop to iterate and add each number to the total, then divide by the length of the list to get the mean.

Step-by-step explanation:

To find the mean of a list of numbers in Python without using an inbuilt function, you need to write a code that will perform the arithmetic manually. The general formula for calculating the mean is the sum of all the numbers divided by the count of the numbers. Here is a step-by-step example of how you can calculate the mean manually in Python:

  • Create a list of numbers: numbers = [1, 2, 3, 4, 5]
  • Initialize a variable to hold the sum of the numbers: total_sum = 0
  • Use a for loop to iterate through each number in the list and add it to total_sum: for number in numbers: total_sum += number
  • The count of the numbers is simply the length of the list, which you can find with count = len(numbers)
  • Finally, divide the total_sum by the count to find the mean: mean = total_sum / count

This will give you the mean of the numbers in the list without using any built-in functions like sum() or mean() from various Python libraries.

User DominikAmon
by
7.1k points