Answers for "python input matrix"

3

how to get matrix element in the form of matrix in python

# A basic code for matrix input from user 
  
R = int(input("Enter the number of rows:")) 
C = int(input("Enter the number of columns:")) 
  
# Initialize matrix 
matrix = [] 
print("Enter the entries rowwise:") 
  
# For user input 
for i in range(R):          # A for loop for row entries 
    a =[] 
    for j in range(C):      # A for loop for column entries 
         a.append(int(input())) 
    matrix.append(a) 
  
# For printing the matrix 
for i in range(R): 
    for j in range(C): 
        print(matrix[i][j], end = " ") 
    print()
Posted by: Guest on April-28-2020
0

Take Matrix input from user in Python

# Taking one row at a time with space-separated values.
# And converting each of them to using map and int function.

matrix = []
for i in range(row):
   col = list(map(int, input().split()))
   matrix.append(col)
print(matrix)

'''
Input :
1 2
3 4
Output :
[[1, 2], [3, 4]]
'''
Posted by: Guest on September-30-2021
1

how to input 2-d array in python

matrix = [input().split() for i in range(no_of_rows)] # if only row is given and the number of coloumn has to be decide by user
matrix= [[input() for j in range(no_of_cols)] for i in range(no_of_rows)] # if both row and coloumn has been taken as input from user
Posted by: Guest on April-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language