How to Find the Average of 3 Numbers in Python

How to find the average of 3 numbers in Python? We will discuss all possible methods to find average of three numbers in python. We will give three numbers num1, num2, and num3. Python program will calculate average of those numbers using various methods.

How to find the average of numbers

Average Formula = Total sum of all numbers / Number of item in the set

Mathematically,

Inputs: a=2, b=5, c=8
Average = (a+b+c)/3 = (2+5+8)/3 = 15/3 = 5

Now let’s see how we can calculate average of three numbers in Python.

Python Program to Find the Average of Three Numbers

This is the simplest and easiest way to find the average of 3 numbers program in Python. We will take three numbers while declaring the variables and its average value will be stored to avg variable, and finally, it will be displayed on the screen.

Program description:- Write a Python program to find the average of three numbers

# Python program to find average of three numbers

# take inputs
num1 = 3
num2 = 5
num3 = 14

# calculate average
avg = (num1 + num2 + num3)/3

# display result
print('The average of numbers = %0.2f' %avg)

Output:-

The average of numbers = 7.33

In this program, we have hardcoded the values of numbers num1, num2 and num3 in the source code, 3, 5, and 14 numeric values are assigned to them.

num1 = 3
num2 = 5
num3 = 14

Calculate the average of three numbers using average formula.

avg = (num1 + num2 + num3)/3

Finally, display the average value of those numbers using print() function.

print('The average of numbers = %0.2f' %avg)

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to find average of three numbers

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))

# calculate average
avg = (num1 + num2 + num3)/3

# display result
print('The average of numbers = %0.2f' %avg)

Output for the different input values:-

Enter first number: 2
Enter second number: 5
Enter third number: 8
The average of numbers = 5.00

Enter first number: 12
Enter second number: 9.63
Enter third number: 22.3
The average of numbers = 14.64

In this program, inputs are scanned using the input() function and stored in variable num1, num2, and num3.

num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))

Calculate the average of three numbers using the average formula and finally, display the average value of those numbers using print() function.

Find the Average of three Numbers using Functions

We can also take the help of a function to find the average of 3 numbers in python. A function is a block of code that performs a specific task.

# Python program to find average of three numbers using function

def avg_num(num1, num2, num3):   #user-defined function
    avg = (num1 + num2 + num3)/3   #calculate average
    return avg    #return value

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))

# function call
average = avg_num(num1, num2, num3)

# display result
print('The average of numbers = ',average)

Output:-

Enter first number: 8.5
Enter second number: 15
Enter third number: 20
The average of numbers = 14.5

In this program, we will be the first defined function.

def avg_num(num1, num2, num3):   #user-defined function
    avg = (num1 + num2 + num3)/3   #calculate average
    return avg    #return value

Inputs are scanned using the input() function and stored in variable num1, num2 and num3. Then call the function and print the average of those numbers.

Also See:- Python Program to Add Two Numbers

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply