Python Program to Print an Identity Matrix

Problem: write a python program to print an identity matrix of order nxn.

Solution: This python program accepts an integer or we can a number from user. It uses two for loops to loop for rows and columns. Whenever row no and column no are equal this progr prints 1 otherwise it prints 0. An identity matrix is a matrix in which all the diagonal elements are one and non diagonal elements are zero.


A 3x3 identity matrix would look like

1  1  0
0  1  0
0  0  1

Python program to print identify matrix

num=int(input("Enter any integer: "))
for i in range(0,num):
    for j in range(0,num):
            print("1",end=" ")
            print("0",end=" ")


Enter any integer: 3

1 0 0 
0 1 0 
0 0 1