×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python program to create matrix using numpy

Here, we are going to learn how to create a matrix (two-dimensional array) using numpy in Python programming language?
Submitted by IncludeHelp, on March 26, 2020

By using numpy class we can create a matrix using two ways.

  1. Using numpy.array()
    mat = numpy.array([[10,20,30],[40,50,60],[70,70,90]])
    
  2. Using numpy.matrix()
    mat = numpy.matrix("10 20 30; 40 50 60; 70 80 90")
    

Consider the below program,

# Python matrix creation using numpy

# importing the numpy
import numpy as np

# creating matrix using numpy.array()
mat1 = np.array([[10,20,30],[40,50,60],[70,70,90]])
# printing matrix
print("mat1...")
print(mat1)

# creating matrix using numpy.matrix()
mat2 = np.matrix("10 20 30; 40 50 60; 70 80 90")

# printing matrix
print("mat2...")
print(mat2)

Output

mat1...
[[10 20 30]
 [40 50 60]
 [70 70 90]]
mat2...
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Python Array Programs »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.