How to Find the Average of 4 Numbers in Python

We will develop a program on how to find the average of 4 numbers in Python. 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 calculate the average of four numbers 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=4, b=2, c=8, d=6
Average = (a+b+c+d)/4 = (4+2+8+6)/4 = 20/4 = 5

Python Program to Calculate the Average of 4 Number

This Python program is the simplest and easiest way to calculate the average of 4 numbers. We will take four 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 four numbers

# take inputs
num1 = 1
num2 = 3
num3 = 5
num4 = 9

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

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

Output:-

The average of numbers = 4.50

Average of 4 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, num3, and num4.

Program description:- Write a python program which input four numbers and display their average.

# Python program to find average of four numbers

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

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

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

Output:-

Enter first number: 5
Enter second number: 12
Enter third number: 4
Enter four number: 30
The average of numbers = 12.75

Enter first number: 5.3
Enter second number: 12
Enter third number: 0.8
Enter four number: 10
The average of numbers = 7.03

Find the Average of 4 Numbers using Functions

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

# Python program to find average of four numbers using function

def avg_num(num1, num2, num3, num4,):   #user-defined function
    avg = (num1 + num2 + num3+ num4) / 4   #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: '))
num4 = float(input('Enter four number: '))

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

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

Output:-

Enter first number: 25
Enter second number: 12
Enter third number: 3.9
Enter four number: 7
The average of numbers = 11.975

Take 4 integers from Keyboard using a Loop and Print their Average value on the Screen in Python

This python program also performs the same task but with different methods. In this program, we are using For Loop to take inputs and calculate the sum of those numbers. Then, calculate the average of those numbers and finally, print the average value.

# Python program to find the average of four numbers

# denotes total sum of four numbers
total_sum = 0

for n in range (4):
    # take inputs
    num = float(input('Enter number: '))
    # calculate total sum of numbers
    total_sum += num

# calculate average of numbers
avg = total_sum / 4

# print average value
print('Average of numbers =', avg)

Output:-

Enter number: 4.5
Enter number: 8
Enter number: 36
Enter number: 7.6
Average of numbers = 14.025

Also See:- How to Find the Average of 10 Numbers in Python

Leave a Comment

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