Python Practice › Basic Programs › Program 5
Average of given numbers
Write a Python program to find the average of given numbers. Average = sum of the numbers / how many numbers there are.
Input
a = 40, b = 50, c = 30
Output
Average = 40.0
Approach
- Add the numbers.
- Divide the sum by the count (3).
Solution 1: three variables
a = 40b = 50c = 30 average = (a + b + c) / 3print("Average =", average)Output
Average = 40.0
Line by line
Line 5
The brackets make Python add first and then divide. Without them, only c would be divided by 3.
Solution 2: any number of values
numbers = [40, 50, 30]average = sum(numbers) / len(numbers)print("Average =", average)Output
Average = 40.0
Line by line
Line 2
sum() adds every value and len() counts them, so this works for a list of any size.
Note
/ always returns a float, which is why the answer prints as 40.0.
Learn the concept
New to this topic? Read the lesson, then come back.