# How to Create a Matrix in Python

How to Create a Matrix in Python | Here, we will discuss how to create a matrix in python? A matrix is a rectangular table arranged in the form of rows and columns. In the programming world, we implement a matrix by using arrays by classifying them as 1D and 2D arrays. In this section, there are some examples to create a matrix in python. Also See:- List in Python MCQ

We will see these below Python program examples to create a Matrix:

1. How To Create Matrix In Python Using Numpy
2. How To Create A Matrix In Python Without Numpy
3. How To Create A Matrix In Python Using For Loop
4. How To Create An Empty Matrix In Python
5. How To Create A Zero Matrix In Python
6. How To Create A 2d Matrix In Python
7. How To Create A 3×3 Identity Matrix In Python

## How to Create a Matrix in Python using NumPy

In the Python programming language, NumPy is a library that supports single and multi-dimensional arrays, matrices, and a large collection of high-level mathematical functions to operate on these arrays and matrices. It contains lots of pre-defined functions which can be called on these matrices and it will simplify our task.

### Make a Matrix in Python Using NumPy.array()

Now let us see a simple program to create/make a matrix in Python using NumPy. In this code, we will use numpy.array() to make/create a matrix. NumPy.array() returns an array object satisfying the specified requirements given in the parameter.

``````import numpy as np
num = np.array([[1, 1, 2], [3, 5, 3], [5, 6, 9]])
print(num)``````

Output:-

[ [1, 1, 2]
[3, 5, 3]
[5, 6, 9] ]

In the above code, we have imported NumPy as `np`, then in variable `num`, we stored an array by using the function numpy.array(). However, we can do this in a mathematical way also but it would be a long sequence. That is why using the NumPy library can be a better way of coding in the Python programming language, and makes our task very easy.

### Python Create a Matrix Using NumPy.matrix()

The numpy.matrix() is also a function in NumPy that is used to return an array, it works very similarly to numpy.array().

``````import numpy as np
num = np.matrix(([1, 1, 1], [15, 34, 3], [50, 69, 99]))
print(num)``````

Output:-

`[[1 1 1] [15 34 3] [50 69 99]]`

### Create a Matrix in Python Using NumPy.reshape()

The numpy.reshape() is a function in NumPy that converts a 1D array to 2D.

``````import numpy as np
num = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9]]).reshape(3, 3)
print(num)``````

Output:-

[ [1, 2, 3]
[4, 5, 6]
[7, 8, 9] ]

The reshape() function takes 2 parameters that are row and column. It is basically used to alter the size of the array. In the above example reshape() takes 2 parameters 3 and 3 so it converts 1D array to 2D array 3X3 elements.

### Make a Matrix in Python Using NumPy.append()

The numpy.append() function appends an array into the row. It will append values to the end of an array.

``````import numpy as np
num = np.array([[5, 7, 8]])
new = np.append(num, [[4, 5, 6], [4, 9, 0], [2, 3, 1]], axis=0)
print(new)``````

Output:-

[ [5, 7, 8]
[4, 5, 6]
[4, 9, 0]
[2, 3, 1] ]

In the above code, the array num is appended to new so the array gets altered. The axis parameter is set 0 as we wish to append the elements as rows.  We have also used shape as it displays the dimension of the matrix. In the above example, we have created a matrix with 4 rows and 3 columns hence dimension is (4,3).

## How to Create a Matrix in Python Without NumPy

NumPy library contains various methods to create a matrix but it also can be created without using NumPy. The below code creates a 2D matrix using the nested list. A nested list is a list within a list.

``````m = [[5, 3, 7], [7, 6, 7], [1, 6, 0]]
for i in m:
print(i) ``````

Output:-

[5, 3, 7]
[7, 6, 7]
[1, 6, 0]

In the example shown we have declared variable m as a nested list and then used a for loop to print all the elements, without a for loop the elements have been printed in a single line.

We can do this by using a join function

``````m = ([5, 3, 7], [7, 6, 7], [1, 6, 0])
for i in m:
print("". join(str(i)))``````

Output:-

[5, 6, 7]
[7, 6, 7]
[1, 6, 0]

The above shown both examples give the same output but the difference is join() function joins the elements of a row into a single string before printing it.

## How to Create a Matrix in Python Using For Loop

We can also use for loop to create a matrix.

Step 1: We have first a single list `mat`
Step 2: Then we iterate by for loop to print it twice using a range within the list it will change into nested list acting as a matrix

``````mat = [[3, 8, 9] for i in range(2)]
for i in mat:
print("".join(str(i)))``````

Output:-

[3, 8, 9]
[3, 8, 9]

In the above output, we have printed the list twice by giving the range parameter as 2.

## How to Create an Empty Matrix in Python

In the below-shown example we have used a library called NumPy, there are many inbuilt functions in NumPy which makes coding easy.  Here we have used one such function called empty() which creates an empty matrix.

``````import numpy as np
emp = np.empty((0, 4))
print(emp)``````

Output:-

[ ]

The above code has created a matrix with 0 rows and 4 columns.

## How to Create a Zero Matrix in Python

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

``````import numpy as np
mat = np.zeros([2, 2], dtype=int)
print(mat)``````

Output:-

[[0, 0]
[0, 0]]

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

## How to Create a 2D Matrix in Python

We can create a 2D matrix in python by using a nested list.

``````m = [[4, 1, 2], [7, 5, 3], [9, 6, 9]]
for i in m:
print("".join(str(i)))``````

Output:-

[4, 1, 2]
[7, 5, 3]
[9, 6, 9]

The above output shows a 2-dimensional matrix.

## How to Create a 3X3 Identity Matrix in Python

A 3X3 matrix is a matrix that has 3 rows and 3 columns, and an identity matrix is a matrix whose diagonal elements are always 1. The function np.identity() itself creates a identity matrix of 3 rows and 3 columns.

``````import numpy as np
a = np.identity(3)
print(a)``````

Output:-

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!