Find the average of an unknown number of inputs in Python
Last Updated :
07 Apr, 2023
Prerequisites: *args and **kwargs in Python
The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyword, variable-length argument list. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args. In this article, the task is to find the average of the unknown number of inputs.
Examples:
Input : 1, 2, 3
Output : 2.00
Input : 2, 6, 4, 8
Output: 5.00
Below is the implementation.
Python3
# function that takes arbitrary
# number of inputs
def avgfun(*n):
sums = 0
for t in n:
sums = sums + t
avg = sums / len(n)
return avg
# Driver Code
result1 = avgfun(1, 2, 3)
result2 = avgfun(2, 6, 4, 8)
# Printing average of the list
print(round(result1, 2))
print(round(result2, 2))
Output:
2.0
5.0
Time complexity: O(n), where n is the number of inputs passed to the function.
Auxiliary space: O(1), as the amount of memory used by the function does not increase with the size of the input.
Method 2: Use the built-in Python function sum() to calculate the sum of the inputs and then divide it by the length of the input to get the average.
Python3
# function that takes arbitrary
# number of inputs
def avgfun(*n):
return sum(n) / len(n)
# Driver Code
result1 = avgfun(1, 2, 3)
result2 = avgfun(2, 6, 4, 8)
# Printing average of the list
print(round(result1, 2))
print(round(result2, 2))
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Use the reduce() function from the functools module.
Step-by-step approach:
- Import the functools module.
- Define a function that takes two arguments, a and b, and returns their sum.
- Use the reduce() function to sum all the input numbers.
- Divide the sum by the length of the input to get the average.
- Return the average.
Below is the implementation of the above approach:
Python3
import functools
def avgfun(*n):
sum_of_inputs = functools.reduce(lambda a, b: a + b, n)
avg = sum_of_inputs / len(n)
return avg
result1 = avgfun(1, 2, 3)
result2 = avgfun(2, 6, 4, 8)
print(round(result1, 2))
print(round(result2, 2))
Time complexity: O(n), where n is the number of input numbers, because it loops through all the inputs once to calculate the sum.
Auxiliary space: O(1) auxiliary space because it only uses a constant amount of memory to store the sum and the average.
Method 4: Using mean() from statistics module in python where we need to pass the list as an argument which will give the average as an output for the given list.
Step-by-step approach:
- Import the statistics module.
- Use the mean() function to get average.
- Print the average.
Python3
import statistics
result1 = statistics.mean([1, 2, 3])
result2 = statistics.mean([2, 6, 4, 8])
print(result1)
print(result2)
Time complexity: O(n), where n is the number of input numbers.
Auxiliary space: O(1)
Similar Reads
Find Average of a Number Digits in Python AIn this article, we will see how to find the average of the digits of a number in Python. Examples: Input: N = 642 Output: 4.0 Explanation: (6+4+2)/3 = 12/3 = 4.0 Input: N = 3504 Output: 3.0 Explanation: (3+5+0+4)/4 = 12/4 = 3.0Calculate the average of a number of digits in python if the number is
3 min read
Find average of a list in python We are given a list of numbers and the task is to find the average (or mean) value of its elements. For example, if we have a list nums = [10, 20, 30, 40], the sum of the elements is 10 + 20 + 30 + 40, which equals 100. The number of elements in the list is 4. So, the average is calculated as 100 /
2 min read
Python | Find Mean of a List of Numpy Array Given a list of Numpy array, the task is to find mean of every numpy array. Let's see a few methods we can do the task. Method #1: Using np.mean() Python3 1== # Python code to find mean of every numpy array in list # Importing module import numpy as np # List Initialization Input = [np.array([1, 2,
1 min read
How to Compute the Average of a Column of a MySQL Table Using Python? A MySQL connector is needed to generate a connection between Python and the MySQL server. Here we will import mysql.connector library to get the average of the specified column in the given database. If you need to know how to install MySQL, see How to Install MySQL in Python 3. Average Function of
2 min read
Multiply All Numbers in the List in Python Our task is Multiplying all numbers in a list Using Python. This can be useful in calculations, data analysis, and whenever we need a cumulative product. In this article we are going to explore various method to do this. Using a loopWe can simply use a loop (for loop) to iterate over the list elemen
2 min read