We will develop a Python program to find the average of two numbers. The average is defined as the mean value which is equal to the ratio of the sum of the number of a given set of values to the total number of values present in the set. Here, we will develop the python average of two numbers program using various methods.
How to find the average of numbers:
Average Formula = Total sum of all numbers / Number of item in the set
Average = (x1+x2+x3+…+xn)/n
Mathematically,
Inputs: a=3, b=5
Average = (a+b)/2 = (3+5)/2 = 8/2 = 4
Python Average of Two Numbers
This Python program is the simplest and easiest way to calculate the average of 2 numbers. We will take two numbers while declaring the variables and calculate the average value of those numbers using the average formula. Then, the average value will be displayed on the screen.
# Python program to find average of two numbers
# first number
num1 = 10
# second number
num2 = 20
# calculate average of those numbers
avg = (num1 + num2) / 2
# print average value
print('The average of numbers = %0.2f' %avg)
Output:-
The average of numbers = 15.00
Average of 2 Numbers in Python
In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. Inputs are scanned using the input() function and stored in variable num1, num2.
# Python program to find average of two numbers
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
# calculate average of those numbers
avg = (num1 + num2) / 2
# print average value
print('The average of numbers = %0.2f' %avg)
Output for the different input values:-
Enter first number: 3
Enter second number: 7
The average of numbers = 5.00
Enter first number: 5.5
Enter second number: 23
The average of numbers = 14.25
Average of Two Numbers in Python using Function
We can also take the help of a function to find the average of 2 numbers in python. A function is a block of code that performs a specific task.
# Python program to find average of two numbers using function
def avg_num(num1, num2): #user-defined function
avg = (num1 + num2) / 2 #calculate average
return avg #return value
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
# function call
average = avg_num(num1, num2)
# display result
print('The average of numbers = %0.2f' %average)
Output:-
Enter first number: 25
Enter second number: 48
The average of numbers = 36.50
Average of Two Numbers using a Loop
This python program also performs the same task but with different methods. In this program, we are using the For Loop to take inputs and calculate the sum of those numbers. Then, calculate the average of numbers and finally, print the average value.
# Python program to find the average of two numbers
# denotes total sum of numbers
total_sum = 0
for n in range (2):
# take inputs
num = float(input('Enter number: '))
# calculate total sum of numbers
total_sum += num
# calculate average of numbers
avg = total_sum / 2
# print average value
print('Average of numbers = %0.2f' %avg)
Output:-
Enter number: 20
Enter number: 30
Average of numbers = 25.00
Also See:- How to find the average of 3 numbers in Python
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
#What is this?
Lines starting with # are comments. It is ignored by the Python compiler.
Super.