- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Take Matrix input from user in Python
In this tutorial, we are going to learn how to take matric input in Python from the user. We can take input from the user in two different ways. Let's see two of them.
Method 1
Taking all numbers of the matric one by one from the user. See the code below.
Example
# initializing an empty matrix matrix = [] # taking 2x2 matrix from the user for i in range(2): # empty row row = [] for j in range(2): # asking the user to input the number # converts the input to int as the default one is string element = int(input()) # appending the element to the 'row' row.append(element) # appending the 'row' to the 'matrix' matrix.append(row) # printing the matrix print(matrix)
Output
If you run the above code, then you will get the following result.
1 2 3 4 [[1, 2], [3, 4]]
Matrix 2
Taking one row at a time with space-separated values. And converting each of them to using map and int function. See the code.
Example
# initializing an empty matrix matrix = [] # taking 2x2 matrix from the user for i in range(2): # taking row input from the user row = list(map(int, input().split())) # appending the 'row' to the 'matrix' matrix.append(row) # printing the matrix print(matrix)
Output
If you run the above code, then you will get the following result.
1 2 3 4 [[1, 2], [3, 4]]
Conclusion
If you have queries in the tutorial, mention them in the comment section.
- Related Articles
- Make JavaScript take HTML input from user, parse and display?
- How to take user input using HTML forms?
- Python Get a list as input from user
- How to take input in Python?
- Taking input from the user in Tkinter
- How to input multiple values from user in one line in Python?
- How to get Input from the User in Golang?
- Swift Program to Get Input from the User
- Java Program to Get Input from the User
- Haskell program to get input from the user
- Can we read from JOptionPane by requesting input from user in Java?
- How to create input Pop-Ups (Dialog) and get input from user in Java?
- Get number from user input and display in console with JavaScript
- How to input multiple values from user in one line in C#?
- How to input multiple values from user in one line in Java?

Advertisements