Skip to content

Introduction to NumPy

What is NumPy?

NumPy (Numerical Python) is the most important library for numerical computing in Python.

It provides:

  • A fast, memory-efficient array object: ndarrayndarray
  • Vectorized operations (operate on whole arrays without Python loops)
  • Linear algebra, randomness, statistics, and more

NumPy is the foundation for many data tools:

  • Pandas
  • SciPy
  • Scikit-learn
  • Matplotlib

Why NumPy is fast

Python lists are flexible, but they’re not optimized for heavy numeric work.

NumPy arrays are fast because:

  • They store data in contiguous memory blocks
  • They have a fixed data type (dtypedtype)
  • Many operations run in optimized C code under the hood

Installing NumPy

With pip

command
pip install numpy
command
pip install numpy

With conda

command
conda install numpy
command
conda install numpy

Importing NumPy

The standard import alias is npnp:

import
import numpy as np
import
import numpy as np

Check version:

version
import numpy as np
print(np.__version__)
version
import numpy as np
print(np.__version__)

NumPy arrays vs Python lists

Python list example

list
a = [1, 2, 3]
b = [4, 5, 6]
 
# This concatenates lists (not element-wise addition)
print(a + b)  # [1, 2, 3, 4, 5, 6]
list
a = [1, 2, 3]
b = [4, 5, 6]
 
# This concatenates lists (not element-wise addition)
print(a + b)  # [1, 2, 3, 4, 5, 6]

NumPy array example

array
import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
 
# Element-wise addition
print(a + b)  # [5 7 9]
array
import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
 
# Element-wise addition
print(a + b)  # [5 7 9]

Key concepts

1) ndarrayndarray

A NumPy array is called an n-dimensional array (ndarrayndarray). It can represent:

  • 1D data (vector)
  • 2D data (matrix)
  • 3D+ data (tensors)

2) shapeshape

The shape tells you the number of rows/columns (dimensions):

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

3) dtypedtype

dtypedtype is the element type (int, float, etc.):

dtype
arr = np.array([1, 2, 3])
print(arr.dtype)
dtype
arr = np.array([1, 2, 3])
print(arr.dtype)

A tiny analytics-style example

Imagine you have daily sales and want quick math:

sales
import numpy as np
 
sales = np.array([100, 120, 90, 150, 130])
 
print("Total:", sales.sum())
print("Average:", sales.mean())
print("Max:", sales.max())
print("Min:", sales.min())
sales
import numpy as np
 
sales = np.array([100, 120, 90, 150, 130])
 
print("Total:", sales.sum())
print("Average:", sales.mean())
print("Max:", sales.max())
print("Min:", sales.min())

Common mistakes

Mistake 1: Mixing numbers and strings

NumPy will try to choose a single dtype. If you mix types, it may convert everything to strings.

Mistake 2: Using Python loops for large data

NumPy is built for vectorization. Prefer array operations over forfor loops.

Next

Continue to: NumPy Array Creation to learn all the ways to build arrays (from lists, zeros/ones, ranges, random data, and more).

πŸ§ͺ Try It Yourself

Exercise 1 – Create a NumPy Array

Exercise 2 – Array Shape and Reshape

Exercise 3 – Array Arithmetic

If this helped you, consider buying me a coffee β˜•

Buy me a coffee

Was this page helpful?

Let us know how we did