Skip to content
Related Articles

Related Articles

Find the average of an unknown number of inputs in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 03 Dec, 2021

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-keyworded, 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.

Input : 1, 2, 3
Output : 2.00

Input : 2, 6, 4, 8
Output: 5.00

Below is the implementation. 


# 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))




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!