# How to find average of n numbers in Python

In this post, you will learn how to use simple logic to find the **average of n numbers** using the **Python** programming language. There are different ways to find the average of numbers. Here, we have mentioned most of them.

## Python program to find average of first n natural numbers

To find the average of the first **n** natural numbers. In the given **Python** program, we have used the **input()** function to accept an integer number from a user. Next, we used the **for loop** to iterate over the **range()**. In each iteration, we will get the next number and calculate the sum until we reach the last number. Finally, we have divided the calculated sum by the total number of inputs taken.

```
num = int(input('How many numbers: '))
total = 0
for n in range(num):
numbers = float(input('Enter the number : '))
total += numbers
avg = total/num
print('Average of ', num, ' numbers is :', avg)
```

**Output of the above code-**

```
How many numbers: 5
Enter the number : 23
Enter the number : 42
Enter the number : 23
Enter the number : 54
Enter the number : 12
Average of 5 numbers is : 30.8
```

Instead of using a **for loop**, we can also take advantage of the inbuilt **sum()** function to calculate the sum of the first **n** natural numbers, and then we can calculate the average.

```
num = int(input('How many numbers: '))
total_sum = sum(range(1, num + 1))
avg = total_sum /num
print('Average of ', num, ' numbers is :', avg)
```

**Output of the above code-**

```
How many numbers: 3
Average of 3 numbers is : 2.0
How many numbers: 5
Average of 5 numbers is : 3.0
```

## Python program to find average of first n natural numbers using while loop

In the given **Python** program, we have found the average of the first **n** natural numbers using a **while loop**.

```
print("Enter the number: ", end="")
n = int(input())
print("Enter " +str(n)+ " Numbers: ", end="")
nums = []
i = 0
while i < n:
nums.append(int(input()))
i = i+1
total_sum = 0
i = 0
while i < n:
total_sum = total_sum+nums[i]
i = i+1
avg = total_sum/n
print("\nAverage of ",n," numbers is ", avg)
```

**Output of the above code-**

```
Enter the number: 5
Enter 5 Numbers: 23
12
53
23
65
Average of 5 numbers is 35.2
```

## Python program to find average of multiple user inputs

The following **Python** program demonstrates how to find the average of multiple user-entered numbers.

```
input_string = input('Enter numbers separated by space ')
print("\n")
# Splits the input numbers into list
nums = input_string.split()
# Convert each item to int type
for i in range(len(nums)):
nums[i] = int(nums[i])
# Calculating the sum and average
print("The sum of numbers : ", sum(nums))
print("The average of numbers : ", sum(nums) / len(nums))
```

**Output of the above code-**

```
Enter numbers separated by space 23 54 43 32 64 23
The sum of numbers : 239
The average of numbers : 39.833333333333336
```

## Python program to find average of n numbers using recursion function

Here is the **Python** program to find the average of numbers using the **recursion** function. When a user enters a number as input, it passes to the function. If the number equals 0, it returns 0. If the input is greater than 0, the function calls recursively and finds the average of **n** natural numbers.

```
# Python Program to find Sum and Average of N Natural Numbers
def average_of_numbers(num):
if(num == 0):
return num
else:
return (num + average_of_numbers(num - 1))
num = int(input("Please enter the number : "))
total = average_of_numbers(num)
average = total / num
print("\n")
print("Sum of numbers from 1 to ",num," is ",total)
print("Average of numbers from 1 to ",num," is ",average)
```

**Output of the above code-**

```
Please enter the number : 6
Sum of numbers from 1 to 6 is 21
Average of numbers from 1 to 6 is 3.5
```

### Related Articles

**Reverse pyramid pattern in PythonFizzbuzz program in Python Python split string by comma Python alive progress bar Python progress bar tqdm Python language translator code Python take screenshot of specific window Web scraping Python BeautifulSoup Reverse a number in Javascript HTML open link in new tab Python print without newline Python iterate list with index Python add list to list Python random choice Python dict inside list Remove character from string Python Python OpenCV Image Filtering Eye Detection Program in Python OpenCV Python Tkinter Combobox Multiply all elements in list Python**