How to Calculate the Mean Value of List Items in Python

One of the most important tasks in data analysis is determining the mean value of a set of values. This may be accomplished in Python by using basic programming methods and resources. This tutorial will provide a simple and instructive guide on calculating the average of items within a list using Python. Regardless of the level of knowledge, This tutorial aims to provide you with the knowledge to comprehend and execute a calculation of averages through basic Python coding.

Python Tutorial: How to Compute the Mean of a List”

 

Here Paste the pic: The Pic is

1. Method: By using Numpy
import numpy as np
  • This line imports the NumPy library, which offers assistance for an array of mathematical functions, such as the capability to calculate the average of a set of numbers.
# Giving input the list by the user

list = eval(input("Enter the integer value for list: "))
  • This line prompts the user to input a list of integers.
  • ‘input(“Enter the integer value for list: “) displays a message to the user and takes their input as a string value.
  • An input string is evaluated as a Python expression using the ‘eval()’ function, resulting in a list of integers.
# This function is for finding Mean Value

def Mean(list):
    # average function
    mean = np.average(list)
    return(mean)
  • This block defines a function named ‘Mean’ that takes a list as its parameter.
  • The function ‘np. average(list)’ inside calculates the average value or mean of the list elements by utilizing NumPy’s ‘average’ function.
  • We return the mean value after making the calculation.
# Final value print and the function called

print("Mean of the list =", round(Mean(list), 2))
  • This line calls the ‘Mean’ function, passing the user input list as an argument.
  • The Mean function rounds the result (mean value) to two decimal places using the ’round()’ function.
  • The console displays the message ‘Mean of the list =’ followed by the rounded mean value.
Entire Code:
#------------1st Method-------------------------

# importing numpy module

import numpy as np

# Giving input the list by the user

list = eval(input("Enter teh integer value for list: "))

#  This funtion is for finding Mean Value

def Mean(list):
    # average function
    mean = np.average(list)
    return(mean)

# Final value print and the function called

print("Mean of the list =", round(Mean(list), 2))
Output:

# If the given input is:[10,20,30,40,50,60]

Ans: Mean of the list = 35.0
2. Method: By using Statistics
import statistics
  • This line imports the ‘statistics’ module, which provides functions for mathematical statistics of numeric data, including the calculation of the mean.
# Sample list

list = eval(input("Enter the integer value for list: "))
  • The code prompts the user to enter a list of integers.
  • The ‘input’ function displays the message “Enter the integer value for list: ” and takes the user’s input as a string.
  • The ‘eval’ function evaluates this input string as a Python expression, converting it into an actual list of integers.
# Calculate mean
mean_value = statistics.mean(list)
  • The code calculates the mean (average) value of the list elements by calling ‘statistics. mean’ with the user input list as an argument.
  • It stores the calculated mean value in the variable ‘mean_value’.
print(f"The mean value is: {mean_value}")
  • The code prints the mean value to the console.
  • It uses an f-string to format the output, displaying the message “The mean value is: ” followed by the calculated mean value.
Entire Code:
#---------------------- 2nd Method ---------------------

import statistics

# Sample list

list = eval(input("Enter teh integer value for list: "))
# Calculate mean
mean_value = statistics.mean(list)

print(f"The mean value is: {mean_value}")
Output:

# If the given input is:[10,20,30,40,50,60]

Ans: Mean of the list = 35.0
3. Method: By using Statistics

 

list = eval(input("Enter the integer value for list: "))  # Here we use this expression for taking :the input's datatype is 'int'.
  • The code prompts the user to enter a list of integers.
  • The ‘input’ function displays the message “Enter the integer value for list: ” and takes the user’s input as a string.
  • The ‘eval’ function evaluates this input string as a Python expression, converting it into an actual list of integers.
length = len(list)  # It will give us length of list
  • The code calculates the length of the list using the ‘len’ function.
  • It stores the length in the variable ‘length’.
mean = sum = 0  # Here we have created a mean variable, and the sum function is using for the sum of the list
  • The code initializes two variables, ‘mean’ and ‘sum’, both set to ‘0’.
  • It will use the ‘sum’ variable to accumulate the sum of the list elements.
for i in range (0, length):
    sum += list[i]
  • The code iterates over the list elements using a ‘for’ loop.
  • It runs the loop from the index ‘0’ to ‘length-1’.
  • In each iteration, it adds the current list element ‘list[i]’ to the ‘sum’ variable.
mean = sum / length  # This is the formula for finding the mean value
  • The code calculates the mean value by dividing the ‘sum’ of the list elements by the ‘length’ of the list.
  • It stores the calculated mean value in the ‘mean’ variable.
print("Given list value is: ", list)  # Here we have printed the list
  • The code prints the original list to the console with the message “Given list value is: “.
print("The mean value of the list is: ", mean)  # Here we have printed the mean value
  • The code prints the mean value to the console with the message “The mean value of the list is: “.
Entire Code:
#------------3rd Method---------------
list = eval(input("Enter the integer value for list: "))  # Here we use this expression for taking :the input's datatype is 'int'.

length = len(list)  #It will give us length of list

mean = sum = 0  # Here we have create a mean variable and the sum function is using for the sum of the list 
for i in range (0,length):
    sum +=list[i]

mean = sum/length   # This is the formula for finding the mean value
print("Given list value is: ",list)  # Here we have printed the list
print("The mean value of the list is: ",mean)   # Here we have print the mean value
Output:

# If the given input is:[10,20,30,40,50,60]

Ans: Mean of the list = 35.0

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top