Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (55.6k points)

Can anyone explain how to take 2d array input in Python?

1 Answer

0 votes
by (119k points)

There are multiple ways to take 2d array input in Python. Here I have mentioned the basic to take 2d array input:

n_rows= int(input("Number of rows:"))

n_columns = int(input("Number of columns:"))

#Define the matrix

matrix = [ ]

print("Enter the entries row-wise:")

#for user input

for i in range(n_rows):          # A for loop for row entries

    a =[ ]

    for j in range(n_cols):      # A for loop for column entries

         a.append(int(input()))

    matrix.append(a)

  

#To print the matrix

for i in range(n_rows):

    for j in range(n_cols):

        print(matrix[i][j], end = " ")

    print( )

Output:

Enter the number of rows:2

Enter the number of columns:3

Enter the entries rowwise:

3

4

5

6

7

8

3 4 5

6 7 8

If you are interested to learn Python from Industry experts, you can sign up this Python Certification by Intellipaat.

You can watch this video on Python tutorial in Hindi by Intellipaat to learn the fundamentals of Python:

by (100 points)
The code is not working properly. Change the second line to:
n_cols = int(input("Number of columns:"))

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 11, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Jul 9, 2020 in Python by ashely (50.2k points)
0 votes
1 answer

Browse Categories

...