Python Program to calculate sum and average of three numbers
Last Updated :
24 May, 2024
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
Similar Reads
How to calculate the average of a set of numbers? Answer: The formula for calculating the average is: Average=Sum of all values/Number of valuesThe mean or average of the given data distribution is the calculated central value. It is used to determine the average of the given data. It gives a measure of the central tendency of the data. The central
6 min read
Find the average of an unknown number of inputs in Python Prerequisites: *args and **kwargs in Python The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyword, variable-length argument list. The syntax is to use the symbol * to take in a variable number of argu
3 min read
Python Program to Find Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid
4 min read
How to Calculate Moving Averages in Python? In this discussion we are going to see how to Calculate Moving Averages in Python in this discussion we will write a proper explanation What is Moving Averages?Moving Averages, a statistical method in data analysis, smooths fluctuations in time-series data to reveal underlying trends. Calculating th
11 min read
Program to calculate the average temperature for the given temperatures Given a list of temperatures on different days, the task is to calculate the average temperature for the given temperatures. Examples: Input: n = 7, temperatures[] = {40, 42, 44 ,40, 39, 46, 45}Output: 42.2857 Explanation: The sum of temperatures is 296 so the average is 296/7 = 42.2857. Input : n =
3 min read