Positive and Negative Number Array - Python Programming Exercise

This Python program prompts the user to enter 10 real numbers and calculates two averages: one for the positive numbers and another for the negative numbers. The program uses a for loop to iterate through each number, checking if it is positive or negative using a conditional statement (if). The program then calculates the average for both groups separately by maintaining cumulative sums and counts for each category. This exercise offers a valuable opportunity for beginners to practice handling real number inputs and working with arithmetic operations. It also reinforces the concept of conditionals for categorizing data and the calculation of averages, which is a common task in many real-world applications.

 Category

Arrays, Lists, and Strings

 Exercise

Positive And Negative Number Array

 Objective

Develop a Python program to prompt the user for 10 real numbers and display the average of the positive ones and the average of the negative ones.

 Example Python Exercise

 Copy Python Code
# Program developed by: Programmer 1, Programmer 2

# Initialize lists to store positive and negative numbers
positive_numbers = []
negative_numbers = []

# Prompt the user for 10 real numbers
for i in range(10):
    number = float(input(f"Enter real number {i+1}: "))
    
    if number > 0:
        positive_numbers.append(number)
    elif number < 0:
        negative_numbers.append(number)

# Calculate and display the average of positive numbers
if positive_numbers:
    positive_average = sum(positive_numbers) / len(positive_numbers)
    print(f"The average of the positive numbers is: {positive_average}")
else:
    print("No positive numbers entered.")

# Calculate and display the average of negative numbers
if negative_numbers:
    negative_average = sum(negative_numbers) / len(negative_numbers)
    print(f"The average of the negative numbers is: {negative_average}")
else:
    print("No negative numbers entered.")

 Output

Enter real number 1: 5.5
Enter real number 2: -3.2
Enter real number 3: 7.8
Enter real number 4: -4.5
Enter real number 5: 1.2
Enter real number 6: -2.3
Enter real number 7: 6.0
Enter real number 8: -8.9
Enter real number 9: -1.0
Enter real number 10: 2.4
The average of the positive numbers is: 4.18
The average of the negative numbers is: -4.18

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

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.

  •  Numerous Numbers and Their Sum

    This Python program prompts the user to enter several numbers, calculating their sum as they go. The program continues to prompt the user for numbers until they enter...

  •  Matrix Array

    This Python programming exercise is ideal for practicing the handling of multidimensional arrays. In this case, the goal is to prompt the user for the marks of 20 students, ...

  •  Advanced Statistics

    This Python statistical program is a fantastic exercise for improving data handling and user interaction in programming. The program allows users to add new data...

  •  Namedtuple

    This Python program is an excellent introduction to using NamedTuples for storing structured data. The program allows users to define 2D points with specific f...

  •  Array of Namedtuple

    In this exercise, you will develop a Python program that expands the previous exercise (NamedTuple point), allowing up to 1,000 points to be stored using an "array of...

  •  Array of Namedtuple and menu

    In this exercise, you will develop a Python program that expands the previous exercise (array of NamedTuples), so that it displays a menu where the user...