Average of numbers in Python

Calculating the average of a set of numbers is a common operation in programming, and there are multiple ways to do it in Python. To find the average of a set of numbers in Python, you can add the numbers and divide the sum by the number of elements in the set. Here’s an example to find Average of numbers in Python:

def average(numbers):
    return sum(numbers) / len(numbers)

print(average([1, 2, 3]))  # Output: 2.0
print(average([10, 20, 30]))  # Output: 20.0
print(average([-10, 0, 10]))  # Output: 0.0

In this example, we define the average() function that takes a list of numbers as an argument and returns their average. The function calculates the sum of the numbers using the built-in sum() function and the number of elements in the list using the built-in len() function. Then, it divides the sum by the number of elements and returns the result.

how to find the average of 3 numbers in python

To find the average of three numbers in Python, you can add the numbers and divide the sum by 3. Here’s an example to find the average of 3 numbers in python:

def average(x, y, z):
    return (x + y + z) / 3

print(average(1, 2, 3))  # Output: 2.0
print(average(10, 20, 30))  # Output: 20.0
print(average(-10, 0, 10))  # Output: 0.0

How to find the average of inputs in Python

To find the average of a set of inputs in Python, you can add the inputs and divide the sum by the number of elements in the set. Here’s an example of how to find the average of inputs in Python:

def average():
    numbers = []
    while True:
        number = input("Enter a number (enter 'q' to quit): ")
        if number == 'q':
            break
        numbers.append(int(number))
    return sum(numbers) / len(numbers)

print(average())  # Output: the average of the inputs

In the above example, we define the average() function that prompts the user to enter a number and adds it to a list of numbers. The function continues to ask for inputs until the user enters ‘q’. Then, the function calculates the average of the numbers in the list by dividing the sum of the numbers by the number of elements in the list.

How to find average of 10 numbers in Python

To find the average of 10 numbers in Python, you can add the numbers and divide the sum by 10. Here’s an example of How to find average of 10 numbers in Python:

def average(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10):
    return (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) / 10

print(average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))  # Output: 5.5
print(average(10, 20, 30, 40, 50, 60, 70, 80, 90, 100))  # Output: 55.0
print(average(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80))  # Output: 25.0

Leave a Comment