Advanced Statistics - Python Programming Exercise

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, view all the data entered, search for a specific item, and display a comprehensive summary of statistics, including the amount of data, sum, average, maximum, and minimum values. The exercise also introduces the concept of menus in Python, where each option is selected by a specific number or letter, enhancing the user interface experience. This is essential for building user-friendly applications. Moreover, the program reserves space for a maximum of 1000 data points, while also keeping track of how many entries have been made. This demonstrates the importance of managing memory and data limits effectively in Python. It provides a practical example of how to store and process large datasets while ensuring that the program remains efficient and responsive. By working through this exercise, users will gain valuable experience in handling dynamic data and creating interactive menu-based applications in Python.

 Category

Arrays, Lists, and Strings

 Exercise

Advanced Statistics

 Objective

Develop a Python statistical program which will allow the user to:

- Add new data
- See all data entered
- Find an item, to see whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program

These options must appear as a menu. Each option will be chosen by a number or a letter.

The program must reserve space for a maximum of 1000 data, but keep count of how many data actually exist.

 Example Python Exercise

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

# Initialize list to store data, with a maximum size of 1000
data = []
max_data_size = 1000

# Main program loop
while True:
    print("\nMenu:")
    print("1. Add new data")
    print("2. See all data entered")
    print("3. Find an item")
    print("4. View summary of statistics")
    print("5. Exit")
    
    choice = input("Choose an option (1-5): ").strip().lower()

    if choice == "1" or choice == "a":
        if len(data) < max_data_size:
            try:
                new_data = float(input("Enter a number to add to the data: "))
                data.append(new_data)
                print("Data added successfully!")
            except ValueError:
                print("Invalid input. Please enter a valid number.")
        else:
            print("Maximum data limit reached. Cannot add more data.")
    
    elif choice == "2" or choice == "b":
        if data:
            print("Data entered so far:")
            for item in data:
                print(item)
        else:
            print("No data entered yet.")
    
    elif choice == "3" or choice == "c":
        try:
            item_to_find = float(input("Enter the number to find: "))
            if item_to_find in data:
                print(f"Item {item_to_find} found in the data.")
            else:
                print(f"Item {item_to_find} not found in the data.")
        except ValueError:
            print("Invalid input. Please enter a valid number.")
    
    elif choice == "4" or choice == "d":
        if data:
            total_data = len(data)
            data_sum = sum(data)
            average = data_sum / total_data
            maximum = max(data)
            minimum = min(data)
            
            print(f"Data Summary:")
            print(f"Amount of data: {total_data}")
            print(f"Sum of data: {data_sum}")
            print(f"Average: {average:.2f}")
            print(f"Maximum: {maximum}")
            print(f"Minimum: {minimum}")
        else:
            print("No data entered yet. Cannot display statistics.")
    
    elif choice == "5" or choice == "e":
        print("Exiting the program.")
        break
    
    else:
        print("Invalid choice, please select a valid option.")

 Output

Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 1
Enter a number to add to the data: 25
Data added successfully!

Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 1
Enter a number to add to the data: 30
Data added successfully!

Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 2
Data entered so far:
25.0
30.0

Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 3
Enter the number to find: 25
Item 25.0 found in the data.

Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 4
Data Summary:
Amount of data: 2
Sum of data: 55.0
Average: 27.50
Maximum: 30.0
Minimum: 25.0

Menu:
1. Add new data
2. See all data entered
3. Find an item
4. View summary of statistics
5. Exit
Choose an option (1-5): 5
Exiting the program.

 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.

  •  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...

  •  Library Database

    In this exercise, you will develop a Python program to create a small database for storing book data. For each book, you will keep the following information: Title...

  •  Enhanced Triangle V2

    In this exercise, you will develop a Python program that prompts the user for their name and displays a triangle with it, starting with 1 letter and growing until it ...

  •  Enhanced Rectangle V3

    In this exercise, you will develop a Python program that prompts the user for their name and a size, and displays a hollow rectangle with it. This exercise is ...