Operations on Arrays

NumPy allows you to perform mathematical operations directly on entire arrays at once, without writing manual loops - a technique called vectorization that makes numerical computation both faster and more readable.

Element-Wise Arithmetic

import numpy as np

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

print(a + b)   # [11 22 33]
print(a * b)   # [10 40 90]
print(b / a)   # [10. 10. 10.]
print(a ** 2)  # [1 4 9]

Broadcasting

Broadcasting lets NumPy perform operations between arrays of different shapes by automatically expanding the smaller array, without actually copying data - saving both time and memory.

import numpy as np

arr = np.array([1, 2, 3])
print(arr + 10)   # [11 12 13] - 10 is "broadcast" across every element

Aggregate Functions

import numpy as np

data = np.array([4, 8, 15, 16, 23, 42])

print(data.sum())    # 108
print(data.mean())   # 18.0
print(data.max())    # 42
print(data.min())    # 4
print(data.std())    # standard deviation
Vectorized NumPy operations aren't just more convenient than loops - they're often 10 to 100 times faster, since NumPy runs the underlying computation in optimized, pre-compiled C code instead of the Python interpreter.

Coming Up Next

Next, you'll learn how to access and work with specific parts of an array using indexing, slicing, and iteration.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course