Python · NumPy Tutorial 2026
NumPy Tutorial for Beginners 2026
Quick summary — NumPy tutorial for beginners
NumPy is the foundation of data science in Python. It gives you fast, powerful array operations that power everything from Pandas to machine learning libraries.
In this tutorial you will learn:
- Creating arrays — different ways to build NumPy arrays.
- Array operations — arithmetic and aggregations.
- Indexing and slicing — accessing and modifying data.
- Broadcasting — perform operations across different shapes.
- Practical examples — real-world data analysis use cases.
SECTION 01Installing NumPy
NumPy is easy to install. If you have Python installed, you can add NumPy with pip or via Anaconda:
# Install NumPy with pip
pip install numpy
# Or install a specific version
pip install numpy==2.0.0
# Verify installation
python -c "import numpy; print(numpy.__version__)"
# Install NumPy with conda (Anaconda or Miniconda)
conda install numpy
# Or create an environment with NumPy
conda create -n myenv python=3.11 numpy
conda activate myenv
# Verify installation
python -c "import numpy; print(numpy.__version__)"
SECTION 02Creating arrays
NumPy arrays (ndarray) are the core data structure. Here are the most common ways to create them:
| Function | Description | Example |
|---|---|---|
np.array() |
Create from a list | np.array([1, 2, 3]) |
np.zeros() |
Array of zeros | np.zeros((2, 3)) |
np.ones() |
Array of ones | np.ones((3, 3)) |
np.arange() |
Range of values | np.arange(0, 10, 2) |
np.linspace() |
Evenly spaced values | np.linspace(0, 1, 5) |
np.random.rand() |
Random numbers | np.random.rand(3, 3) |
import numpy as np
# From a Python list
a = np.array([1, 2, 3, 4, 5])
print(a) # [1 2 3 4 5]
# 2D array (matrix)
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# Array shape and dtype
print(a.shape) # (5,)
print(b.shape) # (2, 3)
print(a.dtype) # int64 (or int32)
import numpy as np
# Zeros and ones
zeros = np.zeros((2, 3))
ones = np.ones((3, 3))
# Range and linspace
r = np.arange(0, 10, 2) # [0 2 4 6 8]
ls = np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1.]
# Random numbers
rand = np.random.rand(3, 3) # uniform [0, 1)
randn = np.random.randn(3) # standard normal
# Identity matrix
eye = np.eye(3)
shape and
dtype — these are the source of most NumPy bugs.
SECTION 03Array operations
NumPy arrays support fast element-wise operations — much faster than Python lists. Here are the key operations:
| Operation | Example | Result |
|---|---|---|
| Element-wise add | a + b |
New array with sums |
| Element-wise multiply | a * b |
New array with products |
| Matrix multiply | a @ b |
Matrix product |
| Sum | a.sum() |
Total of all elements |
| Mean | a.mean() |
Average |
| Standard deviation | a.std() |
Spread measure |
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
# Element-wise operations
print(a + b) # [11 22 33 44]
print(a * b) # [10 40 90 160]
print(a ** 2) # [1 4 9 16]
print(a / 2) # [0.5 1.0 1.5 2.0]
# Matrix multiplication (2D)
m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[5, 6], [7, 8]])
print(m1 @ m2) # [[19 22] [43 50]]
import numpy as np
a = np.array([1, 2, 3, 4])
# Aggregations
print(a.sum()) # 10
print(a.mean()) # 2.5
print(a.min()) # 1
print(a.max()) # 4
print(a.std()) # ~1.118
# Along axis (2D)
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.sum(axis=0)) # [5 7 9] (column sums)
print(m.sum(axis=1)) # [6 15] (row sums)
print(m.mean(axis=0)) # [2.5 3.5 4.5]
SECTION 04Indexing and slicing
NumPy supports familiar Python-like indexing, plus advanced features like boolean and fancy indexing:
| Type | Example | Result |
|---|---|---|
| Basic indexing | a[0] |
First element |
| Slicing | a[1:4] |
Elements 1 to 3 |
| Step slicing | a[::2] |
Every other element |
| 2D indexing | m[1, 2] |
Row 1, column 2 |
| Boolean indexing | a[a > 2] |
Elements greater than 2 |
| Fancy indexing | a[[0, 2, 4]] |
Selected elements |
import numpy as np
a = np.array([10, 20, 30, 40, 50])
# Basic indexing
print(a[0]) # 10
print(a[-1]) # 50
# Slicing
print(a[1:4]) # [20 30 40]
print(a[::2]) # [10 30 50]
# 2D indexing
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m[0, 1]) # 2
print(m[:, 1]) # [2 5] (column 1)
print(m[1, :]) # [4 5 6] (row 1)
import numpy as np
a = np.array([10, 20, 30, 40, 50])
# Boolean indexing
mask = a > 25
print(mask) # [False False True True True]
print(a[mask]) # [30 40 50]
# Fancy indexing
print(a[[0, 2, 4]]) # [10 30 50]
# Modify with mask
a[a > 25] = 0
print(a) # [10 20 0 0 0]
# np.where for conditional selection
b = np.array([1, 2, 3, 4, 5])
result = np.where(b > 3, b, 0)
print(result) # [0 0 0 4 5]
SECTION 05Broadcasting
Broadcasting lets NumPy perform operations on arrays of different shapes, as long as they're compatible. It's one of NumPy's most powerful features.
| Operation | Shapes | Result |
|---|---|---|
| Scalar + array | (3,) + scalar | Element-wise addition |
| 1D + 1D | (3,) + (3,) | Element-wise |
| 2D + 1D | (3, 2) + (2,) | Broadcast along rows |
| 2D + column | (3, 2) + (3, 1) | Broadcast along columns |
| Incompatible | (3, 2) + (3,) | Error |
import numpy as np
# Scalar + array
a = np.array([1, 2, 3])
print(a + 10) # [11 12 13]
# 2D + 1D (broadcast along rows)
m = np.array([[1, 2, 3], [4, 5, 6]])
row = np.array([10, 20, 30])
print(m + row)
# [[11 22 33]
# [14 25 36]]
# 2D + column vector
col = np.array([[100], [200]])
print(m + col)
# [[101 102 103]
# [204 205 206]]
import numpy as np
# Normalise each row of a matrix
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Subtract row means
row_means = data.mean(axis=1, keepdims=True)
print(row_means.shape) # (3, 1)
normalised = data - row_means
print(normalised)
# [[-1. 0. 1.]
# [-1. 0. 1.]
# [-1. 0. 1.]]
# Standardise each column
col_means = data.mean(axis=0)
col_stds = data.std(axis=0)
standardised = (data - col_means) / col_stds
SECTION 06Interview Q&A — NumPy
Q1What is NumPy and why is it used?
NumPy is a Python library for numerical computing. It provides fast, multidimensional arrays and vectorised operations that form the foundation for data science libraries like Pandas, SciPy, and scikit-learn.
Q2Why is NumPy faster than Python lists?
NumPy arrays are stored in contiguous memory and operations are implemented in compiled C — this makes NumPy much faster than Python loops over lists.
Q3What is broadcasting?
Broadcasting lets NumPy perform operations on arrays with different shapes, automatically expanding dimensions where compatible.
Q4What's the difference between
np.array and np.asarray?
np.array creates a new array (copy by default), while
np.asarray returns the input as-is if it's already an
array — no copy.
Q5How do I reshape a NumPy array?
Use a.reshape(new_shape) or a.ravel() for
flattening. Reshape is fast and returns a new view without copying
data.
SECTION 07Test yourself — NumPy quiz
Five questions. No sign-up.
0 / 5Pick an answer to see why it is right or wrong.
SECTION 08Frequently asked questions
Do I need to know Python before NumPy?
Yes — you should be comfortable with Python basics (variables, lists, loops, functions) before diving into NumPy.
Is NumPy used in machine learning?
Yes — libraries like scikit-learn, TensorFlow, and PyTorch use NumPy arrays or compatible structures underneath.
Can I use NumPy for image processing?
Yes — images can be represented as arrays, making NumPy useful for basic image processing tasks.
What's the difference between NumPy and Pandas?
NumPy is for numerical arrays; Pandas is for tabular data with labels. Pandas is built on top of NumPy.
How long does it take to learn NumPy?
With consistent practice, you can learn NumPy basics in 1–2 weeks. Mastering it takes longer and comes with real projects.
SECTION 09Related reads
Classroom & online · Noida
Learn NumPy and Python for data science
Our Data Science with Python Course is designed for beginners — learn NumPy, Pandas, and machine learning from scratch.
₹15,500 · full programme- Beginner-friendly curriculum
- Hands-on projects
- Expert mentorship
- Weekday & weekend batches