Calculating the Mean - Python Programming Exercise

In this exercise, you will develop a Python program that calculates and displays the average of four numbers provided by the user. This task allows you to practice handling user input and performing basic arithmetic operations such as addition and division to calculate the average. You will then use the print function to display the result in a clear and user-friendly format. By completing this exercise, you will strengthen your understanding of Python syntax and how to work with variables and mathematical calculations. This is an essential skill for anyone learning Python, as it helps you develop the ability to process user input and perform simple but valuable calculations. This foundational exercise is an important step toward mastering more advanced programming concepts in Python.

 Category

Your First Steps in Python

 Exercise

Calculating The Mean

 Objective

Develop a Python program that calculates and displays the average of four numbers provided by the user.

 Example Python Exercise

 Copy Python Code
# Prompt the user to enter the first number
num1 = float(input("Enter the first number: "))

# Prompt the user to enter the second number
num2 = float(input("Enter the second number: "))

# Prompt the user to enter the third number
num3 = float(input("Enter the third number: "))

# Prompt the user to enter the fourth number
num4 = float(input("Enter the fourth number: "))

# Calculate the average of the four numbers
average = (num1 + num2 + num3 + num4) / 4

# Display the result
print(f"The average of {num1}, {num2}, {num3}, and {num4} is {average}")

 Output

Enter the first number: 4
Enter the second number: 8
Enter the third number: 6
Enter the fourth number: 10
The average of 4.0, 8.0, 6.0, and 10.0 is 7.0

 Share this Python Exercise

 More Python Programming Exercises of Your First Steps in Python

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Comparative Calculations

    In this exercise, you will develop a Python program to prompt the user for three numbers (a, b, c) and display the result of the expressions (a + b) * c...

  •  Calculating Years

    In this exercise, you will develop a Python program to prompt the user for their age (e.g., 20) and respond with a message like "You look younger than age...

  •  Data Presentation Styles

    In this exercise, you will develop a Python program to prompt the user for a number and display it four times in a row, separated by spaces, and then four time...

  •  Quadrilateral Dimensions

    In this exercise, you will develop a Python program to prompt the user for a number and then display a rectangle 3 columns wide and 5 rows tall using that d...

  •  Conversion

    In this exercise, you will develop a Python program that asks the user for a temperature in Celsius and converts it to both Kelvin and Fahrenheit...

  •  Initial interaction with Python

    In this exercise, you will develop a Python program to output "Hello" on the screen, followed by your name on a separate line. This exercise is perfect for pra...