In this python tutorial, you will learn how to Find Transpose of a Matrix with for loop of the python programming language.
How to Find Transpose of a Matrix?
Let’s take a look at the source code, here the values are assigned in the code, the for loop carry out the function.
RUN CODE SNIPPET# Python Program to Find Transpose of a Matrix X = [[12,7], [4 ,5], [3 ,8]] result = [[0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(X[0])): result[j][i] = X[i][j] for r in result: print(r)
OUTPUT:
[12, 4, 3] [7, 5, 8]
- At first, we enter the values of two matrices. The first one with 3 rows and 2 columns and assign that matrix to the variable
X
. The second one with 2 rows and 3 columns and assign that matrix to the variable Âresult
. - We declare a
for
loop where therange
for the variablei
is(len(X))
- We declare a
for
loop where therange
for the variablej
is(len(X[0]))
for which we give the conditionresult[j][i] = X[i][j]
- We declare the finalÂ
for
loop where theresult
is the variabler
after which we give aprint
function which will display the values stored in the variabler
- In this program, we have used nested
for
loop to iterate through each row and each column. At each point we place theX[i][j]
element intoresult[j][i]
NOTE:
- The print function is used to display the value, it prints the specified message to the screen
-  The For loops are used to loop through an iterable object and perform the same action for each entry.
- The len() is a built function  used to calculate the length of any iterable object