Python Program to Find the Sum and Average Of Three Numbers

Write a Python program to find the sum and average of three numbers. This example accepts three integer values and calculates the sum and average using arithmetic operators.

num1 = int(input("Please Enter the First Number  = "))
num2 = int(input("Please Enter the Second number = "))
num3 = int(input("Please Enter the Third number  = "))


sumOfThree = num1 + num2 + num3

avgOfThree = sumOfThree / 3

print('The sum of {0}, {1} and {2} = {3}'.format(num1,num2, num3, sumOfThree))
print('The Average of {0}, {1} and {2} = {3}'.format(num1,num2, num3, avgOfThree))
Python Program to Find the Sum and Average Of Three Numbers

The below Python program will find the sum and average of three floating point numbers.

num1 = float(input("Please Enter the First Number  = "))
num2 = float(input("Please Enter the Second number = "))
num3 = float(input("Please Enter the Third number  = "))


sumOfThree = num1 + num2 + num3

avg = sumOfThree / 3

flooravg = sumOfThree // 3

print('The sum of {0}, {1} and {2} = {3}'.format(num1,num2, num3, sumOfThree))
print('The Average of {0}, {1} and {2} = {3}'.format(num1,num2, num3, avg))
print('Floor Average of {0}, {1} and {2} = {3}'.format(num1,num2, num3, flooravg))
Please Enter the First Number  = 19.6
Please Enter the Second number = 12.8
Please Enter the Third number  = 156.98
The sum of 19.6, 12.8 and 156.98 = 189.38
The Average of 19.6, 12.8 and 156.98 = 63.126666666666665
Floor Average of 19.6, 12.8 and 156.98 = 63.0

Python Program to find the sum and average of three numbers using functions

In the following program, we define a function that accepts three numbers and returns the sum and average of those numbers.

def averageofthree(a, b, c):
tot = a + b + c
avg = tot / 3
return tot, avg

a = 10
b = 20
c = 30
t, a = averageofthree(a, b, c)
print("Sum of Three = ", t)
print("Average of Three = ",a)
Sum of Three = 60
Average of Three = 20.0

Calculate the sum and average of three numbers using sum()

Here, the built-in sum() function returns the sum of numeric values in a given list. Here, we must build a list using those three numbers and apply the sum() function to it. Refer to the Python list.

a = 20
b = 40
c = 30
tot = sum([a, b, c])
print("Sum of Three = ", tot)
print("Average of Three = ",tot / 3)
Sum of Three = 90
Average of Three = 30.0

Find the sum and average of three numbers using Numpy

When we work with the NumPy module, we can use the built-in sum() and mean() functions. The sum() and mean() functions return the total and average of the three numbers.

import numpy as np
a = 10
b = 40
c = 25
n = [a, b, c]
print("Sum of Three = ", np.sum(n))
print("Average of Three = ",np.mean(n))
Sum of Three = 75
Average of Three = 25.0

Also Read