Matrix Calculator Challenge

Python Exercise of Loops

Python Program to Multiply Matrices

 

Write a Python program that multiplies a 3×3 matrix with a 3×3 matrix. The script should prompt the user to enter the values for both matrices and then calculate the resulting matrix.

Instructions:
1. Ask the user to enter the values for the 3×3 matrix. Each value should be entered on a new line.
2. Ask the user to enter the values for the 3×3 matrix. Each value should be entered on a new line.
3. Calculate the resulting matrix by multiplying the two matrices together.
4. Print the resulting matrix.

Steps

  1. Store the values input by the user in a list of lists.
  2. Store the values entered by the user in another list of lists.
  3. Perform matrix multiplication to calculate the resulting matrix.
  4. Print the resulting matrix.

To create a matrix in Python you can use a list of lists. Each inner list represents a row in the matrix. For example, a 3×3 matrix can be represented as [[a, b, c], [d, e, f], [g, h, i]], where “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h” and “i” are the individual elements of the matrix.

Matrix multiplication involves multiplying the elements of each row of the first matrix with the elements of each column of the second matrix. The resulting matrix will have the number of rows of the first matrix and the number of columns of the second matrix.

This Python exercise of loops is a great opportunity to practice and improve your Python skills about core coding concepts such as:

  1. Input/Output Handling: You need to understand how to prompt users to input matrix values and handle the input data effectively.
  2. Data Structures (Lists): Understanding of lists and nested lists (to represent matrices) is crucial for storing and manipulating matrix elements.
  3. Functions: You will need to define functions to perform matrix multiplication, making use of function parameters and return values.
  4. Nested Loops: Knowledge of nested loops is essential to iterate over the elements of the matrices during multiplication.
  5. Mathematical Operations: Understanding basic mathematical operations in Python is necessary for performing matrix multiplication.

Visit Practity’s real Python projects to keep practicing the rest of Python subjects!

 

Python Exercise Solutions


def input_matrix(rows, cols):
    matrix = []
    print(f"Enter the values for the {rows}x{cols} matrix:")
    for i in range(rows):
        row = []
        for j in range(cols):
            value = float(input(f"Enter value for position ({i+1},{j+1}): "))
            row.append(value)
        matrix.append(row)
    return matrix

def multiply_matrices(matrix1, matrix2):
    result_matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    for i in range(3):
        for j in range(3):
            for k in range(3):
                result_matrix[i][j] += matrix1[i][k] * matrix2[k][j]
    return result_matrix

# Prompt the user to enter the values for the 3x3 matrices
matrix1 = input_matrix(3, 3)
matrix2 = input_matrix(3, 3)

# Multiply the two matrices
result_matrix = multiply_matrices(matrix1, matrix2)

# Print the resulting matrix
print("Resulting Matrix:")
for row in result_matrix:
    print(row)

Output

Enter the values for the 3x3 matrix:
Enter value for position (1,1): 1
Enter value for position (1,2): 2
Enter value for position (1,3): 3
Enter value for position (2,1): 4
Enter value for position (2,2): 5
Enter value for position (2,3): 6
Enter value for position (3,1): 1
Enter value for position (3,2): 2
Enter value for position (3,3): 3
Enter the values for the 3x3 matrix:
Enter value for position (1,1): 1
Enter value for position (1,2): 2
Enter value for position (1,3): 3
Enter value for position (2,1): 3
Enter value for position (2,2): 2
Enter value for position (2,3): 1
Enter value for position (3,1): 2
Enter value for position (3,2): 1
Enter value for position (3,3): 3
Resulting Matrix:
[13.0, 9.0, 14.0]
[31.0, 24.0, 35.0]
[13.0, 9.0, 14.0]

 

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart