Python Program to calculate sum and average of three numbers
Last Updated :
23 Jul, 2025
We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3.
Example:
Input: 10, 20, 30
Output: Sum=60, Average=20.0
Input: 15, 29, 30
Output: Sum=74, Average=24.66
Calculate the Sum and Average of Three Numbers in Python
Now let us see different approaches to calculate the sum and average of three numbers in Python.
Basic Approach
In this approach, we directly initialize three numbers and calculate their sum and average using addition and division arithmetic operators respectively.
Example:
Python
# Initializing three numbers
num1, num2, num3 = 10, 20, 30
# Calculating sum and average
sum_numbers = num1 + num2 + num3
average = sum_numbers / 3
# Displaying the result
print(f'Sum: {sum_numbers}, Average: {average}')
Output:
Sum: 60, Average: 20.0
Using List and Loop
In this approach, we use a Python list to store the three numbers and a for loop which will iterate over each element of the list to calculate sum. This method, requires an additional variable initialized to 0 to store the sum of each element.
Example:
Python
# Using List and Loop
# Initializing three numbers
numbers = [10, 20, 30]
# Variable to store sum
sum_numbers = 0
# Calculating sum
for i in numbers:
sum_numbers += i
# Calculating average
average = sum_numbers / len(numbers)
# Displaying the result
print(f'Sum: {sum_numbers}, Average: {average}')
Output:
Sum: 60, Average: 20.0
Using List and Sum Function
In this approach, we use a list to store the three numbers and use list sum() function to calculate their sum. This is useful for handling more numbers efficiently.
Example:
Python
# Using List and Loop
# Initializing three numbers
numbers = [10, 20, 30]
# Calculating sum and average
sum_numbers = sum(numbers)
average = sum_numbers / len(numbers)
# Displaying the result
print(f'Sum: {sum_numbers}, Average: {average}')
Output:
Sum: 60, Average: 20.0
Using Map Function
In this approach, we declare the three numbers as a Pyhton string, separated by white space. The map() and split() functions are then used to convert the string of numbers into integers. This method is concise and leverages Python's built-in functions.
Example:
Python
# Using Map Function
# Initializing three numbers
num1, num2, num3 = map(int, '10 20 30'.split())
# Calculating sum and average
sum_numbers = num1 + num2 + num3
average = sum_numbers / 3
# Displaying the result
print(f'Sum: {sum_numbers}, Average: {average}')
Output:
Sum: 60, Average: 20.0
Using List Comprehension
In this approach, we use list comprehension to iterate over each element and calculate the sum of the numbers.
Example:
Python
# Using List Comprehension
# Initializing three numbers
numbers = [10, 20, 30]
# Calculating sum and average
sum_numbers = sum([num for num in numbers])
# Calculate the average
average = sum_numbers / len(numbers)
# Displaying the result
print(f'Sum: {sum_numbers}, Average: {average}')
Output:
Sum: 60, Average: 20.0
Using Numpy Module
In this approach we use Python's numpy module. This module has two functions, sum() and mean() which is used to calculate the sum and average of a list respectively.
Example:
Python
# Using numpy module
# Imporyt numpy module
import numpy
numbers = [10, 20, 30]
# Calculating the sum
sum_numbers = numpy.sum(numbers)
# Calculate the average
average = numpy.mean(numbers)
# Displaying the result
print(f'Sum: {sum_numbers}, Average: {average}')
Output:
Sum: 60, Average: 20.0
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice