As we creat a matrix using for loops in python, I would like to know that can we creat it using while loops.
CodePudding user response:
In this link there is the possible solution Python matrix
In reality, the while loop is used to satisfy a condition, instead the for loop is used when we know exactly how many times to execute it. Numpy is also used to create a matrix in Python.
Here is a possible solution, but that’s not the optimal solution:
from random import randint
matrix = []
i=0
while i < 3:
j = 0
n = []
while j < 3:
number = randint(1,100)
n.insert(i,number)
j = j 1
matrix.append(n)
i = i 1
print(matrix)
CodePudding user response:
In this link you can find your answer https://www.geeksforgeeks.org/take-matrix-input-from-user-in-python/
you can also use numpy
import numpy as np
matrix = np.zeros((dim1,dim2))
# this will generate matrix of dim1 x dim2.
you can also refer to numpy documentation.