All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Take 2d Array Input In Python

Last Updated : Mar 11, 2024

How To Take 2d Array Input In Python

In this tutorial we will show you the solution of how to take 2d array input in python, in python programming, when we deal with matrix, the very first step arises is taking input of the matrix from the user.

A two-dimensional array is basically similar to the one-dimensional array. just its visual is as a grid (or table) with rows and columns.

For example, an eight-by-eight grid could be referenced with numbers for each row and letters for each column.

In this tutorial, we’re going to learn the concept of taking input from the user of 2d array in matrix form by going through some easy steps.

Step By Step Guide On How To Take 2d Array Input In Python :-

In this method, we’re going to take input from the user of a matrix but in two dimensional.

x = int(input('number of rows you want '))
y = int(input('number of columns you want'))
mat = []; columns = []
for i in range(0,x):
  mat += [0]
for j in range (0,y):
  columns += [0]
for i in range (0,x):
  mat[i] = columns
for i in range (0,x):
  for j in range (0,y):
    print ('entry in row: ',i+1,' column: ',j+1)
    mat[i][j] = int(input())
print (mat)
  1. First, we took input from the user of how many numbers or rows and columns user want for the matrix.
  2. Declared two variables as ‘x’ and ‘y’ and took input through them by using input function - x = int(input(‘’)) y = int(input(‘’))
  3. Now, we created an array of name ‘mat’ and ‘column’ in order to store the input data from the user within it.
  4. To initialize the number of rows, we used i as variable and the array of named mat by using range to limit it - for i in range(0,x): mat += [0]
  5. Similarly we initialized the number of columns by referring to ‘j’ variable by using the same attribute - for j in range (0,y): columns += [0]
  6. Now we initialized the matrix which was named as ‘mat’ within the code for i in range (0,x) mat[i] = columns for i in range (0,x): for j in range (0,y):
  7. At the end, we initialized the final values and used both the arrays by using the print function - print ('entry in row: ',i+1,' column: ',j+1) mat[i][j] = int(input())
  8. Here we stored the data from i and j array, and initialized it with the mat named array and set it equal to the input values of matrix named ‘mat’ which we got earlier from the user.
  9. And the final step, we printed the result to showcase the user about the values he gave input for - print (mat)
  10. Since we already did initialized the values of i and j with the mat named array, at last we just printed it.

Conclusion :-

In this tutorial, we’ve learned how to take input from the user in two dimensional matrix in python programming.

By following all the above steps, we can easily follow up the conditions of taking input.

Just don’t forget to manipulate the values and declaring correct arrays along with their right terms and initialization.

I hope this tutorial on how to take 2d array input in python helps you.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪