All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Average Of Two Numbers In Python

Last Updated : Mar 11, 2024

Average Of Two Numbers In Python

In this tutorial we will show you the solution of average of two numbers in python, an average is defined as a finding by adding all the numbers in a set together and then dividing them by the quantity of numbers in the list.

Total sum / Total number of terms = Average of two numbers So, let’s get into it.

Step By Step Guide On Average Of Two Numbers In Python :-

Python provides the following approaches that you can use in your program to find the average of any two numbers :

  • Using the standard method
  • Using User-Input
  • Using Function
number1 = 5
number2 = 10
average = (number1 + number2) / 2
print("Average of two numbers = %.2f" %average)
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
average = (number1 + number2) / 2
print("Average of two numbers = %.1f" %average)
def calcAverage(num1, num2):
    return (num1 + num2)/2
average = calcAverage(number1, number2)
print("Average of two numbers = %.2f" %average)
  1. We will discuss all three approaches one by one in this article. The first approach is very simple.
  2. In the first two lines, there are two variables named number1 and number2. These variables have values of 5 and 10 assigned to them, respectively.
  3. In the next line, there is another variable named “average”, which stores the result of the expression. The expression will first perform the addition of those two numbers and then divide them by 2.
  4. In the next line, there is a print statement that will display the value of the average variable. The %.2f format specifier will limit the value of average to 2 decimal places.
  5. In the next two lines, there are two more variables which will store the value entered by the user.
  6. The input() function will take the input from the user as a string datatype, and the float() function will convert the string datatype into a float datatype.
  7. Then the average will be calculated from the two numbers that are inserted by the user, and the value will be stored in the variable average.
  8. In the next line, there is a print statement that will display the value of the variable average. The %.1f format specifier will limit the value of average to 1 decimal place.
  9. In the last approach, you can use a user-defined function to perform the average operation of two numbers. The elements are the numbers that are taken as input from the user.
  10. In the next line, there is a user-defined function named calcAverage() that will accept two parameters, num1 and num2. This function will return the result of the average operation.
  11. In the next line, there is a variable named average that will store the value that is returned from the function call of the calcAverage() function. We will pass the variables number1 and number2 as the parameters to this function call.
  12. In the next line, there is a print statement that will display the value of the variable average.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now find the average of any two numbers in a Python program.

You can use any of the methods mentioned above that you find easy to understand.

The simplest method is the first approach in this article that you can use in your program.

I hope this tutorial on average of two numbers in python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪