Matrix Array - Python Programming Exercise

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, organized into two groups of 10, using a two-dimensional array. The task involves entering the grades of each student and then calculating the average for each group. This type of exercise is essential for improving skills in list manipulation and understanding data structures in Python, which are crucial for developing more complex applications and for data analysis in real-world environments. By developing this program, programmers will also learn to perform mathematical operations on two-dimensional arrays, a key concept in programming.

 Category

Arrays, Lists, and Strings

 Exercise

Matrix Array

 Objective

Develop a Python program to prompt the user for marks for 20 students (2 groups of 10, using a two-dimensional array), and display the average for each group.

 Example Python Exercise

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

# Initialize a 2D list to store marks for 2 groups of 10 students each
marks = []

# Loop through each group (2 groups)
for group in range(2):
    group_marks = []  # Temporary list to store marks for the current group
    print(f"Enter marks for Group {group + 1} (10 students):")
    
    # Loop through each student in the group
    for student in range(10):
        while True:
            try:
                mark = float(input(f"Enter mark for student {student + 1}: "))
                if mark < 0 or mark > 100:
                    print("Please enter a valid mark between 0 and 100.")
                else:
                    group_marks.append(mark)  # Add the mark to the group's list
                    break
            except ValueError:
                print("Please enter a valid number for the mark.")  # Handle invalid input
                
    marks.append(group_marks)  # Add the group's marks to the main list

# Calculate and display the average for each group
for group_index in range(2):
    group_average = sum(marks[group_index]) / len(marks[group_index])
    print(f"The average mark for Group {group_index + 1} is: {group_average:.2f}")

 Output

Enter marks for Group 1 (10 students):
Enter mark for student 1: 85
Enter mark for student 2: 90
Enter mark for student 3: 78
Enter mark for student 4: 88
Enter mark for student 5: 92
Enter mark for student 6: 79
Enter mark for student 7: 84
Enter mark for student 8: 77
Enter mark for student 9: 90
Enter mark for student 10: 89
Enter marks for Group 2 (10 students):
Enter mark for student 1: 68
Enter mark for student 2: 75
Enter mark for student 3: 80
Enter mark for student 4: 82
Enter mark for student 5: 70
Enter mark for student 6: 76
Enter mark for student 7: 79
Enter mark for student 8: 72
Enter mark for student 9: 84
Enter mark for student 10: 67
The average mark for Group 1 is: 84.20
The average mark for Group 2 is: 75.30

 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.

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

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