How To Find The Average Of 3 Numbers In Python
Last Updated : Jan 1, 2023
IN - Python
In this tutorial we will show you the solution of how to find the average of 3 numbers in python, the average or arithmetic mean of a collection of numbers is calculated by adding the numbers and then dividing them by the number of numbers in the collection.
The arithmetic mean is used in almost all scientific disciplines for analysis.
For example, consider the collection of numbers - 6, 6, 6. The arithmetic mean, or you can say that the average is (6+6+6)/3 = 6.
Step By Step Guide On How To Find The Average Of 3 Numbers In Python :-
Python provides some methods that you can use in your program to find the average of any three numbers.
- Using calculations
- Using the for loop
- Using the built-in functions
number1 = int(input("Please first number:")) number2 = int(input("Please second number:")) number3 = int(input("Please third number:")) average = (number1 + number2 + number3) / 3 print("The average of three numbers: " , average) numbers_str = input("Enter numbers separated by spaces: ") numbers = [float(num) for num in numbers_str.split()] total = 0; for x in numbers: total = total + x print("Average of ", numbers, " is ", total / len(numbers)) # Using the built-in functions print("Average of ", numbers, " is ", sum(numbers) / len(numbers))
- In the first approach, we will use basic mathematics to calculate the average of three numbers.
- In the first three lines, there are three variables named number1, number2, and number3.
- These variables will store the values that are given by the user in the input() function. The input() method will take input from the user. The input taken from the user will be of a string datatype.
- We have to convert that string datatype into an integer datatype to perform this operation. The int() function will convert the input given by the user into an integer datatype.
- In the next line, there is a variable named “average”, which will store the value of the expression. Python will first execute the addition operation of three numbers because the addition operation takes place within the brackets.
- After that, it will divide the addition of those three numbers by 3 and store the result in the variable average.
- In the next line there is a print statement which will print the value of the average variable.
- In the next approach, you can use a for loop to calculate the addition of numbers and then divide them by the total number of number of elements.
- In the next line, there is a variable named numbers_str, which will store the values inserted by the user. In the next line, there is a list variable named “numbers”, which will store the individual elements entered by the user.
- In the next line, there is a variable named total which is initialized to the value of 0. In the next line, there is a for loop that will iterate through the list numbers and sum all elements into the total variable.
- In the next line, the print statement will print the average of the numbers by dividing the total by the length of the numbers list.
- In the next line, there is a comment saying that “Using the built-in functions” is the third method you can use.
- In the next line there is just one print statement which will perform addition and division of the list numbers and print the average. The sum() function will add all the elements of the list numbers.
So finally, in conclusion, we can say that with the help of this tutorial, you can now find the average of any three numbers in a Python program.
You can use any of the methods mentioned above that you find easy to understand. All the methods will work fine.
I hope this tutorial on how to find the average of 3 numbers in python helps you and the steps and method mentioned above are easy to follow and implement.