More

    Python Program to Calculate the Average of Numbers in a Given List

    Python Lists

    Lists are one of the most important data structures in Python. They are used to store a sequence of items, similar to an Array in other languages. However, unlike arrays, lists are dynamic and can grow and shrink as needed.

    Program Code

    # Take the number of elements to be stored in the list as input
    n = int(input("Enter number of elements in list: "))
    
    # Use a for loop to input elements into the list
    a = []
    for i in range(0, n):
        elem = int(input("Enter element: "))
        a.append(elem)
    
    # Calculate the total sum of elements in the list
    total = sum(a)
    
    # Divide the sum by total number of elements in the list
    avg = total / n
    
    # Print the result
    print("Average of elements in the list: ", round(avg, 2))

    Input Given:

    Enter number of elements in list: 5
    Enter element: 10
    Enter element: 20
    Enter element: 30
    Enter element: 15
    Enter element: 25

    Expected Output:

    Average of elements in the list: 20.0

    Code Explanation

    1. We take the number of elements to be stored in the list as input from the user.
    2. We use a for loop to input elements into the list.
    3. We calculate the total sum of elements in the list.
    4. We divide the sum by the total number of elements in the list.
    5. We print the result.
    Disclaimer: While we make every effort to update the information, products, and services on our website and related platforms/websites, inadvertent inaccuracies, typographical errors, or delays in updating the information may occur. The material provided on this site and associated web pages is for reference and general information purposes only. In case of any inconsistencies between the information provided on this site and the respective product/service document, the details mentioned in the product/service document shall prevail. Subscribers and users are advised to seek professional advice before acting on the information contained herein. It is recommended that users make an informed decision regarding any product or service after reviewing the relevant product/service document and applicable terms and conditions. If any inconsistencies are observed, please reach out to us.

    Latest Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Join our newsletter and stay updated!