Average of given numbers

EasyBasic Programs · 5 of 40

Write a Python program to find the average of given numbers. Average = sum of the numbers / how many numbers there are.

Advertisement
Input
a = 40, b = 50, c = 30
Output
Average = 40.0

Approach

  1. Add the numbers.
  2. Divide the sum by the count (3).

Solution 1: three variables

Python
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

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs