Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

Introduction to NumPy

Learn what NumPy is, why it’s used in data analytics, and how arrays enable fast numerical computing in Python.

What is NumPy?

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

It provides:

  • A fast, memory-efficient array object: ndarray
  • 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 (dtype)
  • Many operations run in optimized C code under the hood
diagram Why NumPy beats a Python loop mermaid
Contiguous memory plus a fixed dtype let NumPy hand the whole array to a C loop instead of looping element-by-element in Python.

Installing NumPy

With pip

Shell
pip install numpy

With conda

Shell
conda install numpy

Importing NumPy

The standard import alias is np:

Python
import numpy as np

Check version:

Python
import numpy as np
print(np.__version__)

NumPy arrays vs Python lists

Python list example

Python
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

Python
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) ndarray

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

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

2) shape

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

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

3) dtype

dtype is the element type (int, float, etc.). Every value in an array shares the same dtype — that uniformity is exactly what makes vectorized math possible:

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

A tiny analytics-style example

Imagine you have daily sales and want quick math:

Python
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 for loops.

Check your understanding

quizCheck yourself
  1. Which pair of reasons explains why NumPy arrays are fast?

    Show answer

    A — They are stored in contiguous memory with a fixed dtype — Contiguous memory and one fixed dtype let NumPy run optimized C loops instead of looping in Python.

  2. For a = [1, 2, 3] and b = [4, 5, 6] as plain Python lists, what does a + b give?

    Show answer

    B — [1, 2, 3, 4, 5, 6] — Lists concatenate. Only NumPy arrays add element by element, giving [5 7 9].

  3. What does arr.shape print for arr = np.array([[1, 2, 3], [4, 5, 6]])?

    Show answer

    B — (2, 3) — The array has 2 rows and 3 columns, so the shape is (2, 3).

  4. For sales = np.array([100, 120, 90, 150, 130]), what is sales.mean()?

    Show answer

    A — 118.0 — The sum is 590 and there are 5 values, so the mean is 118.0.

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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Lists Concatenate, Arrays Add

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Quick Descriptive Stats

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did