How to Create a Zero Matrix in Python

How to Create a Zero Matrix in Python | Previously we have developed programs to create matrix in Python now, we will discuss how to create a zero matrix in python. Matrix is a rectangular table arranged in the form of rows and columns, in the programming world we implement matrix by using arrays by classifying it as 1D and 2D arrays. In this section, there are some examples to create a zero matrix in python.

How to Create a Zero Matrix in Python

A zero matrix is a matrix that contains all 0 elements. The np.zeros() is a function in NumPy that creates a zero matrix, here dtype is used to specify the data type of the elements.

import numpy as np
m = np.zeros([3, 3], dtype=int)
print(m)

Output:-

[[0 0 0]
[0 0 0]
[0 0 0]]

The above code creates a zero matrix with 3 rows and 3 columns.

Python Program to Create a Zero Matrix

This python program also performs the same task but in different ways. In this program, we will create a zero matrix in python without NumPy.

m, n = 2, 3
matrix = [[0] * n for i in range(m)]

Python Program Examples with Output

Python Conversion Programs

Leave a Comment

Your email address will not be published. Required fields are marked *