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 the average of three numbers in python. We will give three numbers num1, num2, and num3. Python program will calculate the average of those numbers using various methods.

How to find the average of numbers (Average formula in Python)

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

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 their average value will be stored to avg variable, and finally, it will be displayed on the screen.

Program description:-Write a Program to Find the Average of 3 Numbers in Python

# 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 the average formula.

avg = (num1 + num2 + num3)/3

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

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

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

Write down a Python Program to take 3 Numbers from the User and Calculate the Average of the Numbers.

# 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 input values test-case-1:-

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

Output for the input values test-case-2:-

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 variables 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 the print() function.

How to Find the Average of 3 Numbers in Python 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:- How to Find the Average of 5 Numbers in Python and Python Program to Find Sum of N Numbers

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!

Follow Us
Instagram • Twitter • Youtube • Linkedin • Facebook • Pinterest • Telegram

Leave a Comment

Your email address will not be published. Required fields are marked *