Tutorials Python Python NumPy Basics
Ch 49 / 58
Python Intermediate 15 min read Chapter 49 of 58

Python NumPy Basics

NumPy (Numerical Python) is the cornerstone of data science in Python. It provides the ndarray — a multi-dimensional, typed array that is 10–100× faster than Python lists for numerical operations. Everything in data science — Pandas, Matplotlib, SciPy, TensorFlow, scikit-learn — builds on NumPy. This chapter teaches you to create arrays, understand shapes and dtypes, index and slice, perform element-wise operations, and leverage broadcasting — the skills every data scientist uses daily.

What you'll learn in this chapter
ndarray — shape, dtype, ndim, size, itemsize
Creating arrays — zeros, ones, arange, linspace
Indexing, slicing and boolean masking
Arithmetic, ufuncs and aggregation
Reshaping — reshape, ravel, flatten, transpose
Broadcasting — operating on different shapes
Advertisement

Why NumPy? Lists vs ndarray

Python lists are flexible but slow for numerical computing — each element is a separate Python object with overhead. NumPy's ndarray stores all elements in a contiguous block of typed memory, enabling C-speed vectorised operations without Python loops.

Python — why_numpy.py
import numpy as np
import time

# ── Speed comparison ──────────────────────────────────
n = 1_000_000

# Python list
py_list = list(range(n))
t0 = time.perf_counter()
result = [x**2 for x in py_list]
print(f"List: {time.perf_counter()-t0:.3f}s")    # ~0.120s

# NumPy array
np_arr = np.arange(n)
t0 = time.perf_counter()
result = np_arr ** 2
print(f"NumPy: {time.perf_counter()-t0:.3f}s")    # ~0.002s — 60× faster!

# ── Memory comparison ─────────────────────────────────
import sys
py_list_small = list(range(1000))
np_arr_small  = np.arange(1000)
print(sys.getsizeof(py_list_small))             # ~8056 bytes
print(np_arr_small.nbytes)                     # 8000 bytes (int64 × 1000)

# ── Key differences ───────────────────────────────────
# Python list:  heterogeneous, dynamic size, slow loops
# NumPy array:  homogeneous type, fixed size, vectorised C ops

# ── Install NumPy ─────────────────────────────────────
# pip install numpy
# In Colab/Jupyter: already installed
print(np.__version__)                           # e.g. 2.0.1

Creating NumPy Arrays

NumPy provides many ways to create arrays — from Python lists, special factory functions, random generators, and file data:

np.array()
From list or nested lists
np.zeros()
All-zero array
np.ones()
All-ones array
np.arange()
Range like range()
np.linspace()
N evenly spaced values
np.eye()
Identity matrix
np.random.rand()
Uniform [0,1) random
np.full()
Filled with a value
Python — creating_arrays.py
import numpy as np

# ── From Python lists ─────────────────────────────────
a1d = np.array([1, 2, 3, 4, 5])
a2d = np.array([[1, 2, 3], [4, 5, 6]])
print(a1d)         # [1 2 3 4 5]
print(a2d)         # [[1 2 3] [4 5 6]]

# ── Specify dtype explicitly ──────────────────────────
f32 = np.array([1, 2, 3], dtype=np.float32)
c64 = np.array([1+2j, 3+4j], dtype=complex)

# ── Factory functions ─────────────────────────────────
print(np.zeros((2, 3)))              # 2×3 zeros
print(np.ones((3, 3)))               # 3×3 ones
print(np.full((2, 4), 7))            # 2×4 filled with 7
print(np.eye(3))                      # 3×3 identity matrix

# ── Range arrays ──────────────────────────────────────
print(np.arange(10))                  # [0 1 2 3 4 5 6 7 8 9]
print(np.arange(0, 1, 0.2))          # [0.  0.2 0.4 0.6 0.8]
print(np.linspace(0, 1, 5))          # [0.   0.25 0.5  0.75 1.  ]

# ── Random arrays ─────────────────────────────────────
rng = np.random.default_rng(42)      # seeded for reproducibility
print(rng.random((2, 3)))            # 2×3 uniform [0,1)
print(rng.integers(0, 10, (3, 3)))  # 3×3 random ints 0-9
print(rng.normal(0, 1, 5))           # 5 values from N(0,1)

Array Attributes — Shape, dtype, ndim, size

Every ndarray has a set of metadata attributes that describe its structure and type. Understanding these is essential for debugging and writing correct array code:

ndarray key attributes — for a 2D array shape (3, 4)
.shape
Tuple of dimension sizes
Number of elements along each axis. (3, 4) = 3 rows × 4 columns.
arr.shape → (3, 4)
.dtype
Data type of all elements
All elements have the same type: float64, int32, bool, complex128 etc.
arr.dtype → float64
.ndim
Number of dimensions (axes)
1D array → 1, 2D matrix → 2, 3D tensor → 3.
arr.ndim → 2
.size
Total number of elements
Product of all shape dimensions: 3×4 = 12.
arr.size → 12
.nbytes
Total memory in bytes
size × itemsize. float64 = 8 bytes/element → 12×8 = 96 bytes.
arr.nbytes → 96
Python — array_attributes.py
import numpy as np

arr = np.array([[1.0, 2.0, 3.0, 4.0],
                 [5.0, 6.0, 7.0, 8.0],
                 [9.0, 10.0, 11.0, 12.0]])

print("shape:   ", arr.shape)           # (3, 4)
print("dtype:   ", arr.dtype)           # float64
print("ndim:    ", arr.ndim)            # 2
print("size:    ", arr.size)            # 12
print("itemsize:", arr.itemsize)        # 8  (bytes per element)
print("nbytes:  ", arr.nbytes)          # 96

# ── Common dtypes ─────────────────────────────────────
# np.int8   np.int16   np.int32   np.int64
# np.float16 np.float32 np.float64 (default)
# np.bool_   np.complex64  np.complex128
# np.str_    np.object_

# ── Type casting ──────────────────────────────────────
ints   = np.array([1, 2, 3])           # int64 (default)
floats = ints.astype(np.float32)       # cast to float32
bools  = ints.astype(bool)             # [True, True, True]
print(floats.dtype)                     # float32
Advertisement
K2infocom Python Masterclass & Backend Development
Master Python, Django & Backend APIs with AI
Go from absolute beginner to building production-ready REST APIs, web applications, and ML pipelines. Real projects, live mentoring in Vadodara, and placement support included.
2,400+
Students Enrolled
4.9 ⭐
Google Rating
6 Months
Full Program
100%
Placement Support
Python 3 NumPy & Pandas Data Science Machine Learning Django & DRF Docker & AWS Real Projects AI Tools

Indexing, Slicing & Boolean Masking

NumPy supports Python-style indexing and slicing, extended to multiple dimensions. Boolean masking is particularly powerful — select elements based on a condition without loops.

Python — indexing_slicing.py
import numpy as np

a = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])

# ── Single element access ─────────────────────────────
print(a[0, 0])             # 1  (row 0, col 0)
print(a[2, 1])             # 8  (row 2, col 1)
print(a[-1, -1])          # 9  (last row, last col)

# ── Row/column slicing ────────────────────────────────
print(a[0])               # [1 2 3]  — first row
print(a[:, 1])            # [2 5 8]  — second column (all rows)
print(a[1:, 1:])          # [[5 6] [8 9]] — bottom-right 2×2
print(a[::2])             # [[1 2 3] [7 8 9]] — every other row

# ── Slices are VIEWS, not copies ─────────────────────
view = a[0]               # view of row 0 — modifying it changes a!
view[0] = 99
print(a[0])               # [99 2 3] — a was modified!
copy = a[0].copy()        # explicit copy — safe

# ── Boolean masking ───────────────────────────────────
scores = np.array([45, 78, 92, 33, 88])
mask   = scores > 60         # [False, True, True, False, True]
print(scores[mask])         # [78 92 88] — passing scores
print(scores[scores > 60])  # same, inline

# ── Fancy indexing ────────────────────────────────────
print(scores[[0, 2, 4]])   # [45 92 88] — specific indices

# ── np.where() ────────────────────────────────────────
grade = np.where(scores >= 60, "Pass", "Fail")
print(grade)                # ['Fail' 'Pass' 'Pass' 'Fail' 'Pass']

Arithmetic, ufuncs & Aggregations

NumPy operations are element-wise by default — no loops needed. Universal functions (ufuncs) are vectorised C implementations of common math operations. Aggregation functions reduce arrays along axes.

Python — numpy_operations.py
import numpy as np

a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])

# ── Element-wise arithmetic ───────────────────────────
print(a + b)                  # [11 22 33 44]
print(a * b)                  # [10 40 90 160]
print(b / a)                  # [10.  10.  10.  10.]
print(a ** 2)                 # [1 4 9 16]
print(a * 10)                 # [10 20 30 40] ← scalar broadcast

# ── Universal functions (ufuncs) ──────────────────────
print(np.sqrt(a))             # [1.   1.41 1.73 2.  ]
print(np.exp(a))              # [e^1, e^2, e^3, e^4]
print(np.log(a))              # [0.   0.69 1.1  1.39]
print(np.abs([-3, 4, -1]))    # [3 4 1]
print(np.sin(np.pi / 2))      # 1.0

# ── Aggregation functions ─────────────────────────────
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.sum())              # 21  (all elements)
print(m.sum(axis=0))        # [5 7 9]  (sum each column)
print(m.sum(axis=1))        # [6 15]   (sum each row)
print(m.mean())             # 3.5
print(m.max(), m.min())    # 6 1
print(m.std(), m.var())    # std=1.87 var=2.92
print(m.argmax())           # 5 (flat index of max)
print(np.median(m))         # 3.5

# ── Matrix operations ─────────────────────────────────
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)                 # matrix multiply  [[19 22] [43 50]]
print(A.T)                   # transpose         [[1 3] [2 4]]
print(np.linalg.det(A))      # determinant -2.0

Reshaping, Stacking & Splitting

Reshaping changes an array's shape without copying data (as long as sizes match). Stacking and splitting combine or divide arrays along axes.

Python — reshape_stack.py
import numpy as np

a = np.arange(12)          # [0, 1, 2, ..., 11]

# ── Reshape ───────────────────────────────────────────
b = a.reshape(3, 4)        # 3 rows × 4 cols
c = a.reshape(2, 2, 3)    # 3D: 2 × 2 × 3
d = a.reshape(-1, 3)      # -1 means "figure it out" → (4, 3)
print(b.shape)             # (3, 4)

# ── Flatten / ravel ───────────────────────────────────
print(b.flatten())         # [0 1 2 ... 11] copy
print(b.ravel())           # [0 1 2 ... 11] view (faster)

# ── Transpose ─────────────────────────────────────────
print(b.T.shape)           # (4, 3) — rows and cols swapped

# ── newaxis — add dimension ───────────────────────────
row = a[np.newaxis, :]     # (1, 12) — row vector
col = a[:, np.newaxis]     # (12, 1) — column vector
print(row.shape, col.shape)# (1, 12) (12, 1)

# ── Stacking arrays ───────────────────────────────────
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

print(np.vstack([x, y]))  # [[1 2 3] [4 5 6]] — stack vertically
print(np.hstack([x, y]))  # [1 2 3 4 5 6]     — stack horizontally
print(np.concatenate([x, y]))  # same as hstack for 1D
print(np.stack([x, y], axis=0))  # new axis: [[1 2 3] [4 5 6]]

# ── Splitting ─────────────────────────────────────────
arr = np.arange(9)
p1, p2, p3 = np.split(arr, 3)
print(p1, p2, p3)          # [0 1 2] [3 4 5] [6 7 8]

Broadcasting — Operating on Different Shapes

Broadcasting is NumPy's mechanism for performing operations on arrays with different shapes. Instead of requiring identical shapes, NumPy "stretches" smaller arrays to match larger ones — without copying memory.

Broadcasting — (3,3) matrix + (3,) row vector
A: shape (3, 3) — [[1,2,3],[4,5,6],[7,8,9]]
+
add row vector b
b: shape (3,) — [10, 20, 30]
▼
b stretched to (3,3) — added to each row
Result: (3,3) — [[11,22,33],[14,25,36],[17,28,39]]
Python — broadcasting.py
import numpy as np

# ── Scalar broadcast ──────────────────────────────────
a = np.array([1, 2, 3])
print(a + 10)               # [11 12 13]  — 10 broadcasts over all
print(a * 2)                # [2 4 6]

# ── Row vector broadcast over matrix ─────────────────
m = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])
row = np.array([10, 20, 30])  # shape (3,)
print(m + row)               # [[11,22,33],[14,25,36],[17,28,39]]

# ── Column vector broadcast ───────────────────────────
col = np.array([[10], [20], [30]])  # shape (3,1)
print(m + col)               # [[11,12,13],[24,25,26],[37,38,39]]

# ── Row × column = outer product ─────────────────────
r = np.array([[1, 2, 3]])   # (1,3)
c = np.array([[1],[2],[3]])  # (3,1)
print(r * c)                 # (3,3) outer product
# [[1 2 3] [2 4 6] [3 6 9]]

# ── Real use: normalize each column ───────────────────
data = np.array([[1.0, 2.0, 3.0],
                   [4.0, 5.0, 6.0]])
col_mean = data.mean(axis=0)   # [2.5, 3.5, 4.5]
col_std  = data.std(axis=0)    # [1.5, 1.5, 1.5]
normalized = (data - col_mean) / col_std  # broadcast!
print(normalized)

Practice Python Online — Free Compilers

Don't want to install anything yet? Practice all NumPy examples from this chapter directly in your browser with these free Python environments. Google Colab and JupyterLite have NumPy pre-installed.

▶
Online Python 3 Compiler
Write, run and debug Python code in your browser. No installation required. Test every NumPy example instantly.
Open Compiler
Replit
Full IDE online
Google Colab
Free GPU notebooks
JupyterLite
Notebooks in browser
Programiz
Beginner-friendly
Chapter Summary
  • NumPy's ndarray stores elements in contiguous typed memory — 10–100× faster than Python lists for numerical operations. Install with pip install numpy, import as np.
  • Key attributes: .shape (dimensions tuple), .dtype (element type), .ndim (number of axes), .size (total elements), .nbytes (memory usage).
  • Create arrays with: np.array(), np.zeros(), np.ones(), np.arange(), np.linspace(), np.random.default_rng().
  • Indexing: a[row, col], slices a[1:3, :]. Slices are views — modifying them changes the original. Use .copy() for an independent copy.
  • Boolean masking: a[a > 0] returns all positive elements. np.where(cond, x, y) is a vectorised if-else.
  • Operations are element-wise by default. Aggregations (sum, mean, max, std) support an axis argument to reduce along rows or columns.
  • Reshaping: reshape() (view), flatten() (copy), ravel() (view), T (transpose). Stacking: vstack, hstack, concatenate.
  • Broadcasting: NumPy stretches smaller-shaped arrays to match larger ones element-wise — no memory copies. Rules: dimensions are matched from the right; missing dims treated as 1.
⚡
Free Resource
Download our Python NumPy Basics Cheat Sheet
Array creation, attributes, indexing, operations, reshaping, broadcasting & ufuncs — one printable page.
Get Sheet
Advertisement
Quick Check — Test Your Understanding
Question & Answer
✓ Answer

.shape returns a tuple that describes the number of elements along each dimension (axis):

  • (5,) — 1D array with 5 elements
  • (3, 4) — 2D array, 3 rows × 4 columns
  • (2, 3, 4) — 3D array, 2 "sheets" × 3 rows × 4 columns

The total number of elements equals the product of all shape dimensions: 3×4 = 12.

✓ Answer

A NumPy slice returns a view — it shares memory with the original array. Modifying the view modifies the original:

a = np.array([1, 2, 3])
view = a[1:]          # view — shared memory
view[0] = 99
print(a)              # [1 99 3] ← original changed!

copy = a.copy()       # independent copy
copy[0] = 0
print(a)              # [1 99 3] ← unchanged
✓ Answer

For a 2D array: axis=0 collapses rows — aggregates along the row direction (down), giving one value per column. axis=1 collapses columns — aggregates along the column direction (across), giving one value per row:

m = np.array([[1,2,3],[4,5,6]])
m.sum(axis=0)  # [5, 7, 9]  ← per column
m.sum(axis=1)  # [6, 15]   ← per row
✓ Answer

Broadcasting is NumPy's way of applying operations between arrays with different (but compatible) shapes. The smaller array is conceptually "stretched" to match the larger one — without actually copying memory.

Rules: shapes are compared from the rightmost dimension. Dimensions are compatible if they are equal or one of them is 1. Missing dimensions are treated as size 1.

Example: (3,4) + (4,) → the (4,) is broadcast to (3,4), adding the row vector to each row.

✓ Answer

Both generate sequences of numbers, but with different controls:

  • np.arange(start, stop, step) — you control the step size, Python determines how many values. Like Python's range() but for floats.
  • np.linspace(start, stop, num) — you control the number of values, NumPy determines the spacing. The stop is inclusive.
np.arange(0, 1, 0.2)     # step=0.2   → [0, 0.2, 0.4, 0.6, 0.8]
np.linspace(0, 1, 5)     # 5 values  → [0, 0.25, 0.5, 0.75, 1.0]
MCQ 1 of 3
What does np.zeros((3, 4)) create?
MCQ 2 of 3
For a 2D array a of shape (4, 5), what does a[:, 2] return?
MCQ 3 of 3
What operator is used for matrix multiplication in NumPy (Python 3.5+)?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel