This Python program calculates the average of numbers in a given list.
Given a list of numbers, we need to find the average of those numbers.
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
- We then calculate the average by dividing the total sum by the number of elements in the list (
- The calculated average is returned from the function.
- An example list
number_listis provided, and the
calculate_averagefunction is called with this list.
- The calculated average is printed to the console.
number_list = [10, 20, 30, 40, 50]