Program to calculate mean of a given list of numbers in Python
In this python program guide, we will learn to:
-
write a program in python to calculate mean of a list of numbers
Program to find mean in python
Given below is code snippet to write a program to calculate mean of a given list of numbers in python.
# Owner : TutorialsInhand Author : Devjeet Roy
numbers = list(map(int, input("Enter numbers: ").strip().split()))
count = 0
total = 0
for i in numbers:
total += i
count += 1
mean = total/count
print("The mean is:",mean)
The output of the write a program to calculate mean of a given list of numbers using list is as follow:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter numbers: 10 20 30 41
The mean is: 25.25
Few important points about this program:
1. The process of finding the mean or averge is to find the sum of the numbers and divide it by the count of numbers.
2. First of all, we take the numbers from the input() function as a string. Then the strip() function eliminates if there is any space in the beginning or in the end of the string. The spilt() function breaks the string spacewise.
3. Each of the string parts, which are basically the numbers in string format, are type casted to int using the map() function.
4. Atlast, we use a list constructor to make a list of integers. Thus we get complete code to find mean in python.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Jan 25,2021