Inverse of a matrix in Python

Written by

Himani Kohli

Inverse of a matrix program in Python

Here, we will learn to write the code for the inverse of a matrix.

We will use numpy.linalg.inv() function to find the inverse of a matrix.

If we multiply the inverse matrix with its original matrix then we get the identity matrix.


  1. Import the package numpy.
  2. An array is initialized using numpy and stored in variable x.
  3. The array is inversed using the function numpy.linalg.inv(x) .
  4. Inversed array is stored in variable y.
  5. Now we’ll print both array x and y.
  6. Exit 


import numpy

x=numpy.array([[1,2], [3,4]])



print (y)



[[1 2]

 [3 4]]

[[-2.   1. ]

 [ 1.5 -0.5]]