- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Explain Python Matrix with examples

A matrix in Python is a two-dimensional array having a specific number of rows and columns. The data elements in Python matrix can be numbers, strings or symbols etc.

Matrix or two-dimensional list is an important data structure. Various operations associated with matrix involve transpose, addition or multiplication of two matrices.

We will discuss how to declare a matrix in python with a specific number of rows and columns and then input data items from the user and finally print the matrix.

## Declare a matrix in Python as nested list

A matrix in Python can be declared as a nested list. The number of rows and columns needs to be specified. Suppose number of rows is 3 and number of columns is 4. We will declare the matrix as follows

Matrix=[[0]*4 for i in range(3)]

## Input matrix elements and print them row-wise

### Example

col=4 rows=3 matrix=[[0]*col for i in range(rows)] for i in range(rows): print("Enter","row",i+1,"elements") for j in range(col): matrix[i][j]=int(input()) def rowvise(matrix): for i in range(rows): print(matrix[i]) print("print as nested list") print(matrix) print("print each row in separate line") rowvise(matrix)

### Output

Enter row 1 elements 1 2 3 4 Enter row 2 elements 5 6 7 8 Enter row 3 elements 1 2 3 4 print as nested list [[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4]] print each row in separate line [1, 2, 3, 4] [5, 6, 7, 8] [1, 2, 3, 4]

These are the basic operations on matrix, which involve declaring a matrix, assigning data element values and printing a matrix. The other operations such as transpose can be achieved by applying specific logic to the given matrix.

- Related Questions & Answers
- Explain Stack in Python with examples
- What is Queue in Python? Explain with examples
- What is an object in Python? Explain with examples
- Explain 5NF with examples in DBMS
- What is JavaScript closure? Explain with examples.
- Explain JavaScript Regular Expression modifiers with examples
- Explain the finally Statement in JavaScript with examples.
- What is context free grammar? Explain with examples
- Explain the concept of DBMS schema with examples?
- What is 1NF in DBMS? Explain with examples
- Explain try and catch statements in JavaScript with examples.
- Explain the Error Name Values in JavaScript with examples.
- How to create constants in JavaScript? Explain with examples.
- Explain string library functions with suitable examples in C
- Explain formal definition of language with examples in TOC?