Python Programming (121 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (64 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)


myMock Interview Service for Real Tech Jobs


Everything You Need To Know About Matrix In Python

Published on Sep 23,2019 27 Views


myMock Interview Service for Real Tech Jobs


myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

This article will introduce you Matrix in Python with every operation that concerns the topic with a programmatic demonstration. Following pointers will be covered in this article,

Let us get started then,

Matrix In Python

Matrix is nothing but a rectangular array of numbers or any other form of data. The basic concept of a matrix should be clear before operating on matrices within the boundaries of python programming language. The horizontal arrangement of data are rows and the vertical arrangement are columns. The size of any matrices or in other words, the number of elements inside a matrix is (R) X (C) where R is rows and C, columns. Python does not have a built in type for matrices, so we consider two or more lists together as a matrix.

Now, let us have a look at viewing elements of a matrix and its functionality. Consider the below illustrated python code. 

print("Below is a Matrixn")
A= [[1,4,5,12],
print("A=", A)
print("nAttempting to print the 2nd rown")
print("A[1] =",  A[1])
print("nAttempting to print the 2nd row, 3rd elementn")
print("A[1][2]=", A[1][2])
print("nPriting last element of the 1st rown")
print("A[0][3] =", A[0][3])
column = [];
for row in A:
print("n Displaying the 3rd column onlyn")
print("3rd column=", column)
print("n Thank you ! Have a nice day!")


Output - Matrix In Python - EdurekaMoving on with this article

NumPy Package For Matrices In Python 

Numpy is a python library, which allows for scientific computing. Numpy can help users work on multidimensional arrays.

/Addition of Matrices
import numpy as np
A= np.array([[24,41],[35,-9]])
B= np.array([[19,-36],[37,68]])
print("Summing a matrix using Numpy is simplen")
print("nThank You!")


/Output - Matrix In Python - Edureka

Moving on with this article

Multiplication Of Matrices

 The product of two matrices is found using Numpy libraries as illustrated below.

Import numpy as np
A= np.array([[7,1,3],[6,-2,0]])
B= np.array([[2,3],[9,5],[4,-2]])
print("nThe product of two matrices is n")
print("nThank you !n")


Output - Matrix In Python - Edureka

Moving on with this article on Matrix In Python,

Transpose Of A Matrix 

Transpose refers to a new matrix formed whose rows are now the columns and whose columns are now the rows of the initial matrix. 

Import numpy as np
print(“n This is your original matrixn”)
print(“this is your transpose”)
print(“nThank You”)


Output - Matrix In Python - EdurekaThis brings us to the end of this article.

To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  article and we will get back to you.


Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.