Last updated

NumPy Introduction

Definition: NumPy (Numerical Python) is the foundational library for numerical computing in Python. Its main object is the ndarray — a grid of values, all of the same type — which is far faster and more memory-efficient than a plain Python list.

Almost every data and machine-learning tool (Pandas, scikit-learn, TensorFlow) is built on top of NumPy. Learning it is the gateway to data science.

Why not just use lists?

NumPy can apply an operation to a whole array at once — no loop needed. This is called vectorisation, and it is both shorter to write and dramatically faster:

import numpy as np
a = np.array([1, 2, 3, 4])
print(a)
print(type(a))
print(a * 2)   # multiplies every element at once

With a list, [1,2,3,4] * 2 just repeats the list. With a NumPy array, it doubles every number.

The convention

NumPy is almost always imported as np: import numpy as np. You will see this everywhere.

💡 Note: NumPy downloads on the first Run (a few seconds), then stays fast for every lesson after.

Try it Yourself
Output

          
Ad · responsive