This Python program calculates the average of numbers in a given list.
Problem Statement:
Given a list of numbers, we need to find the average of those numbers.
Program:
def calculate_average(numbers):
total = sum(numbers)
average = total / len(numbers)
return average
# Example list of numbers
number_list = [10, 20, 30, 40, 50]
avg = calculate_average(number_list)
print("Average:", avg)
How it Works:
- We define a function
calculate_averagethat takes a list of numbers as an argument. - Inside the function, we calculate the sum of all numbers in the list using the
sum()function. - We then calculate the average by dividing the total sum by the number of elements in the list (
len(numbers)). - The calculated average is returned from the function.
- An example list
number_listis provided, and thecalculate_averagefunction is called with this list. - The calculated average is printed to the console.
Input/Output:
Input;
number_list = [10, 20, 30, 40, 50]
Output:
Average: 30.0