NumPy Introduction

Lesson 1 of 17

NumPy is the Python library for working with numbers in bulk. It gives you the ndarray — a fast, multi-dimensional array — and hundreds of functions that work on a whole array at once, without writing loops.

What is NumPy?

NumPy stands for Numerical Python. It was created in 2005 by Travis Oliphant and is free and open source. Almost the entire Python data stack is built on it: pandas, scikit-learn, SciPy, Matplotlib and many deep-learning tools all use NumPy arrays under the hood.

Why not just use Python lists?

A Python list can hold anything — numbers, strings, other lists — which makes it flexible but slow for math. A NumPy array holds one data type in one continuous block of memory, and its operations run in compiled C code. The result is shorter code that, on large data, is often 10 to 100 times faster.

Example

Python
import numpy as np

marks = [72, 85, 90, 64]

# With a list, adding 5 grace marks needs a loop
print([m + 5 for m in marks])

# With NumPy, the operation applies to every element at once
arr = np.array(marks)
print(arr + 5)

Output

Plain Text
[77, 90, 95, 69]
[77 90 95 69]

Notice that NumPy prints arrays without commas. That is how you can tell an array from a list at a glance.

The ndarray object

The array object in NumPy is called ndarray (n-dimensional array). You create one with np.array():

Example

Python
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))

Output

Plain Text
[1 2 3 4 5]
<class 'numpy.ndarray'>

What you will learn in this course

  • Creating arrays and choosing data types

  • Indexing, slicing, reshaping and iterating

  • Joining, splitting, searching, sorting and filtering

  • Vectorized math, broadcasting, random numbers, statistics and linear algebra

Every lesson is short and has runnable examples. Copy any example into Python, Jupyter or Google Colab and run it yourself.