Skip to main content
OperarMatrices.py provides a looping console menu that lets you perform three matrix operations: addition, subtraction, and multiplication. You enter matrix dimensions and values interactively, and the program prints the result before returning you to the menu.

How to run

python OperarMatrices.py
No external dependencies are required — the program uses only the Python standard library (os). When the program starts, you see the following menu on every iteration:
"     Operaciones con matrices     "

"  1) Sumar                        "
"  2) Restar                       "
"  3) Multiplicar                  "
"  0) Salir                        "
Enter 0 at any time to exit. After each operation, the terminal is cleared with os.system('cls') (Windows) and the menu is shown again.
os.system('cls') only clears the terminal on Windows. On Linux or macOS the clear command has no effect and the menu simply reprints below the previous output.

Functions

Prompts you to fill a matrix element by element. When only T1 is passed, it creates a square T1 × T1 matrix. When both T1 and T2 are passed, it creates a rectangular T1 × T2 matrix (T1 rows, T2 columns).
def IngresarMatriz(T1, T2=None):
    if T2 == None:
        matriz = [[0 for _ in range(T1)] for _ in range(T1)]
        for i in range(T1):
            for j in range(T1):
                matriz[i][j] = int(input(f"Ingrese la matriz en ({i},{j}): "))
    else:
        matriz = [[0 for _ in range(T2)] for _ in range(T1)]  # T1 filas, T2 columnas
        for i in range(T1):
            for j in range(T2):
                matriz[i][j] = int(input(f"Ingrese la matriz en ({i},{j}): "))
    return matriz
Prints the result matrix with a header. The *m unpacking removes brackets and commas from each row for clean console output.
def MostrarMatriz(matriz):
    print("*   Matriz resultante   *")
    for m in matriz:
        print(*m)
Accumulates values from matriz into the shared matrizRes result matrix. Both matrices must be square with the same size T.
def SumarMatriz(matriz):
    for i in range(T):
        for j in range(T):
            matrizRes[i][j] += matriz[i][j]
The first matrix in an addition or subtraction session is always passed to SumarMatriz, regardless of the selected operation. This seeds matrizRes with real values instead of operating against a zero matrix.
Subtracts matriz values from matrizRes. Only called for matrices 2, 3, … in a subtraction session.
def RestarMatriz(matriz):
    for i in range(T):
        for j in range(T):
            matrizRes[i][j] -= matriz[i][j]
Multiplies Pmatriz (the first matrix, stored as a global) by matriz (the second) and writes the result into matrizRes. Uses three nested loops implementing the standard dot-product algorithm.
def MultiplicarMatriz(matriz, T1, T2):
    # T1 = rows of first matrix, T2 = columns of second matrix
    # T2P = columns of first = rows of second (global)
    for i in range(T1):       # rows of the first matrix
        for j in range(T2):   # columns of the second matrix
            res = 0
            for k in range(T2P):
                res += Pmatriz[i][k] * matriz[k][j]
            matrizRes[i][j] = res

Operations

How addition works

The program prompts for how many matrices to add (minimum 2), then the shared size T, and then collects each matrix one by one. The first matrix is always added to the zero-initialised matrizRes; subsequent matrices are also added.
You must enter at least 2 matrices. The program will keep re-prompting if you enter a value less than 2.

Walkthrough

1

Select option 1

Seleccione una opcion: 1
2

Enter the number of matrices

Ingrese el numero de matrices: 2
3

Enter the matrix size

Suma de matrices...
Ingrese el tamaño: 2
4

Fill in the first matrix

Ingrese la matriz No. 1:
Ingrese la matriz en (0,0): 1
Ingrese la matriz en (0,1): 2
Ingrese la matriz en (1,0): 3
Ingrese la matriz en (1,1): 4
5

Fill in the second matrix

Ingrese la matriz No. 2:
Ingrese la matriz en (0,0): 5
Ingrese la matriz en (0,1): 6
Ingrese la matriz en (1,0): 7
Ingrese la matriz en (1,1): 8
6

Read the result

*   Matriz resultante   *
6 8
10 12

Important constraints

OperationConstraint
AdditionMinimum 2 matrices; all must be the same square size T × T
SubtractionMinimum 2 matrices; all must be the same square size T × T
MultiplicationExactly 2 matrices; columns of first must equal rows of second

Build docs developers (and LLMs) love