Write a python program to find the average of 10 numbers

1. Introduction

In this blog post, we will create a Python program to calculate the average of 10 numbers. This is a basic exercise in Python that helps in understanding the concepts of loops, lists, and arithmetic operations, foundational elements in any Python programmer's toolkit.

2. Program Steps

1. Initialize a list to hold the 10 numbers.

2. Calculate the sum of all numbers in the list.

3. Divide the sum by the number of elements to find the average.

3. Code Program

def calculate_average(numbers):
    # Step 1: Initialize the sum of numbers
    total_sum = 0

    # Step 2: Calculate the sum of all numbers
    for number in numbers:
        total_sum += number

    # Step 3: Calculate the average by dividing the sum by the number of elements
    average = total_sum / len(numbers)

    return average

# Example usage
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
average = calculate_average(numbers)
print(f"The average of the numbers is: {average}")

Output:

For the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the output will be 5.5, which is the average of these numbers.

Explanation:

1. The function calculate_average takes a list of numbers as an input. In this example, the list contains the first 10 natural numbers.

2. The function initializes a variable total_sum to 0 and then iterates over each number in the list, adding each number to total_sum.

3. After the loop, the sum of all numbers is divided by the length of the list (i.e., the number of elements) using total_sum / len(numbers) to calculate the average.

This program demonstrates a straightforward approach to calculating the average of a set of numbers in Python, showcasing basic arithmetic operations and loop usage.


Comments