Skip to content
Home » Find Average of N Numbers using Function in Python

Find Average of N Numbers using Function in Python

Learn about Find Average of N Numbers using Function in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.

Find Average of N Numbers using Function in Python

In this program, we use a function to calculate the average of n numbers in Python. A function is a piece of code that accomplishes a specific task. The user-defined function in the below code takes a array of input values and returns the average.

Program:

# Python program to find the average of n numbers

#user-defined function
def avg(arr, x):  
    sum = 0
    for i in range(n):
        # calculate total sum of numbers
        sum = sum+arr[i]
    # calculate average of numbers
    avg = sum/x
    return avg

# total number count
n = int(input('How many numbers: '))

# input numbers
num = []
for i in range(n):
    num.append(float(input('Enter number: ')))

# function call
print('The average value of numbers = %0.2f' %avg(num, n))

Output:

How many numbers: 5
Enter number: 1
Enter number: 2
Enter number: 3
Enter number: 4
Enter number: 5
The average value of numbers = 3.00

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Python Program to Find Average of N Numbers using While Loop