Python Program to Find Transpose of a Matrix

This article covers a program in Python that find and prints the transpose of a given 3*3 matrix.

Python Find Transpose of a Given 3*3 Matrix

The question is, write a Python program to find the transpose of a matrix. Elements of matrix must be received by user at run-time of the program. The program given below is its answer:

print("Enter 9 Elements of the Matrix: ", end="")
matrix = []
for i in range(3):
    matrix.append([])
    for j in range(3):
        val = int(input())
        matrix[i].append(val)

print("\nOriginal Matrix:")
for i in range(3):
    for j in range(3):
        print(matrix[i][j], end=" ")
    print()

for i in range(3):
    for j in range(i):
        temp = matrix[i][j]
        matrix[i][j] = matrix[j][i]
        matrix[j][i] = temp

print("\nTranspose of Matrix:")
for i in range(3):
    for j in range(3):
        print(matrix[i][j], end=" ")
    print()

The snapshot given below shows the sample run of above Python program, with user input 1, 2, 3, 4, 5, 6, 7, 8, 9 as nine elements of the matrix:

python program find transpose of matrix

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »