Skip to main content

NumPy Basics

NumPy contains the efficient implementation of some data structures like vectors and matrices. Python does not come with a built-in array structure, so NumPy's array comes in handy.

You may think of a Python list as an array. However, there is a significant difference between a list and an array in Python.

  • List can store different types of data while an array can store same type of data
  • Lists are less-memory efficient while arrays are more memory-efficient and faster for numerical and scientific operations

Open a Jupyter notebook and import the NumPy library.

import numpy as np

Let's see how we can define vectors and matrices. Below is how we define an array.

np.array([1,3,5,7])

array([1, 3, 5, 7])

We can declare a matrix using the below syntax:

a = np.asmatrix([[1,2],[3,3]])
a

matrix([[1, 2], [3, 3]])

We can perform matrix addition, subtraction, and multiplication as below: