Python Program For Average Of 3 Numbers (With Code)

Python Program for Average of 3 Numbers

In this tutorial, you will learn about the Python program for average of 3 numbers.

We will explore a Python program for calculating the average of three numbers.

Python is a popular programming language known for its simplicity and readability.

With just a few lines of code, we can write a program that calculates the average of three given numbers.

So, let’s dive in and see how we can accomplish this!

Introduction

Python Program for Average of 3 Numbers

Below is the Python program for calculating the average of three numbers.

Python Program for Average of 3 Numbers

# Program to calculate the average of three numbers

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

average = (num1 + num2 + num3) / 3

print("The average of the three numbers is:", average)

You can run this code on our free Online Python Compiler.

Output

Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
The average of the three numbers is: 20.0

Let’s break down the program and understand how it works.

  1. We start by taking input from the user for three numbers using the input() function. We use the float() function to convert the input into floating-point numbers, as we want to calculate the average.
  2. Next, we calculate the average by adding the three numbers together and dividing the sum by 3. The result is stored in the average variable.
  3. Finally, we use the print() function to display the average of the three numbers.

Explanation

Explaining the Python Program for Average of 3 Numbers

Now, let’s go through each part of the program and understand its functionality in detail.

Taking Input from the User

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

In this section, we use the input() function to prompt the user to enter three numbers.

The float() function is used to convert the user input into floating-point numbers, as we want to perform arithmetic calculations on them.

Calculating the Average: Python Program for Average of 3 Numbers

average = (num1 + num2 + num3) / 3

In this line of code, we calculate the average by adding the three numbers together and dividing the sum by 3.

We stored the result in the average variable.

Displaying the Result

print("The average of the three numbers is:", average)

Finally, we use the print() function to display the average of the three numbers.

The print() function takes a string and the average variable as arguments.

The string is displayed as it is, and the value of the average variable is inserted using the comma , separator.

FAQs

FAQs About Python Program for Average of 3 Numbers

Q: What is the purpose of this program?

This program is designed to calculate the average of three given numbers using Python.

Q: Can I input decimal numbers?

Yes, you can input decimal numbers.

The program uses the float() function to convert the user input into floating-point numbers, allowing decimal values.

Q: Is the order of input numbers important?

No, the order of input numbers does not affect the calculation of the average.

The program simply adds the three numbers together and divides the sum by 3.

Q: Can I use negative numbers as input?

Yes, you can use negative numbers as input.

The program treats positive and negative numbers equally when calculating the average.

Q: What if I enter non-numeric values?

If you enter non-numeric values, such as letters or symbols, the program will raise a ValueError and display an error message.

Q: Can I calculate the average of more than three numbers?

Yes, you can modify the program to calculate the average of more than three numbers.

Simply adjust the calculation part of the code accordingly, summing up all the numbers and dividing the sum by the total count of numbers.

Q: How do you find the average of 3 numbers in Python?

To find the average of 3 numbers in Python, you can use the formula: sum the three numbers and divide the sum by 3.

Q: How do you write a program with 3 numbers average?

To write a program in Python that calculates the average of 3 numbers, you can take input from the user for the three numbers, calculate their sum, and then divide the sum by 3 to obtain the average.

Q: How do you write an average in Python?

In Python, you can write an average by adding the numbers you want to average together and dividing the sum by the count of numbers.

For example, to find the average of 3 numbers, you can use the formula: sum of numbers divided by 3./

Q: What is the average of 3 numbers?

The average of 3 numbers is the sum of the three numbers divided by 3.

It represents the central value that is equal to the arithmetic mean of the three numbers.

Wrapping Up

Conclusions: Python Program for Average of 3 Numbers

In this article, we learned how to write a Python program to calculate the average of three numbers.

Python’s simplicity and versatility make it easy to perform such calculations with just a few lines of code.

We used the input() function to gather user input, performed arithmetic calculations, and displayed the result with the print() function.

Now you have a solid foundation for creating your own Python programs to calculate averages or perform other mathematical operations.

Happy coding!


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading