Beginner
Introduction to NumPy: Arrays and Array Operations
📂 Phase 5: NumPy, Pandas, Data Processing Basics (Days 21–25) · Python ProgrammingNumPy (Numerical Python) is the foundational library for numerical computing in Python. It introduces the ndarray — a fast, memory-efficient array structure that performs mathematical operations on entire collections of numbers at once, instead of looping through them one by one as you would with a plain Python list.
Installing and Importing NumPy
# Install once from the terminal:
# pip install numpy
import numpy as np # "np" is the universal, near-mandatory alias
Creating a NumPy Array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
print(type(arr)) #
Why Use NumPy Instead of a Python List?
| Aspect | Python List | NumPy Array |
|---|---|---|
| Data types | Can mix types freely | All elements must be the SAME type (homogeneous) |
| Math on the whole collection | Requires a manual loop | Built-in vectorized operations — no loop needed |
| Speed on large datasets | Much slower | Dramatically faster — implemented in optimized C internally |
| Memory usage | Higher overhead per element | Compact, fixed-size storage |
Vectorized Operations — NumPy's Biggest Advantage
prices = np.array([100, 200, 300, 400])
# Apply a discount to EVERY element at once — no loop required
discounted = prices - 20
print(discounted) # [ 80 180 280 380]
# Compare this to a plain list, which would require:
prices_list = [100, 200, 300, 400]
discounted_list = [p - 20 for p in prices_list] # needs a loop or comprehension
Common Ways to Create Arrays
zeros = np.zeros(5) # [0. 0. 0. 0. 0.]
ones = np.ones(4) # [1. 1. 1. 1.]
sequence = np.arange(0, 10, 2) # [0 2 4 6 8] — like range(), but returns an array
even_split = np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1. ] — 5 evenly spaced values
Array Attributes
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # (2, 3) — 2 rows, 3 columns
print(arr.ndim) # 2 — number of dimensions
print(arr.size) # 6 — total number of elements
print(arr.dtype) # int64 — the data type of every element
Element-Wise Arithmetic
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]
Every arithmetic operator works element-by-element automatically across the whole array — this is called vectorization, and it is the single biggest reason NumPy is so much faster than equivalent Python list code for numerical work.
Useful Aggregate Functions
| Function | Returns |
|---|---|
| np.sum(arr) | Sum of all elements |
| np.mean(arr) | Average value |
| np.max(arr) / np.min(arr) | Largest / smallest value |
| np.std(arr) | Standard deviation |
scores = np.array([85, 90, 78, 92, 88])
print(np.mean(scores)) # 86.6
print(np.max(scores)) # 92
Interview tip: "Why is NumPy faster than a regular Python list for numerical operations?" is asked constantly — the core answer is that NumPy arrays store data in contiguous memory blocks of a single fixed type, and operations are implemented in pre-compiled C code, avoiding the overhead of Python's per-element type-checking that a list-based loop would incur.