📘 NumPy: Complete Interview-Ready Notes with
Examples
🧠 What is NumPy?
NumPy (Numerical Python) is a powerful Python library used for scientific and numerical computations. It
provides:
• A fast, memory-efficient N-dimensional array object called ndarray
• Functions for advanced mathematical, statistical, logical, and algebraic operations
• Tools for reshaping, broadcasting, and manipulating arrays
✅ Key Features
• Supports large multi-dimensional arrays and matrices
• Optimized C backend makes it faster than native Python
• Essential for libraries like Pandas, Scikit-learn, TensorFlow, etc.
🔄 NumPy Array vs Python List
Feature NumPy Array Python List
Size Fixed Dynamic
Data Type Homogeneous Heterogeneous
Speed Faster (C-based) Slower
Memory Efficiency High Low
Broadcasting Support Yes No
📌 Example
import numpy as np
arr = [Link]([1, 2, 3]) # NumPy array
lst = [1, 2, 3] # Python list
print(arr)
1
✅ Output:
[1 2 3]
🏗️ Array Creation Examples
import numpy as np
print([Link]([1, 2, 3])) # Basic array
print([Link]([1, 2, 3], dtype=float)) # Float array
print([Link](1, 11, 2)) # Range with step size
print([Link](16).reshape(2, 2, 2, 2)) # 4D array
print([Link]((3, 4))) # 3x4 matrix of zeros
print([Link](4)) # 4x4 Identity matrix
print([Link]((3, 4))) # 3x4 matrix with random
values
print([Link](-10, 10, 10, dtype=int)) # 10 values from -10 to
10
✅ Output:
[1 2 3]
[1. 2. 3.]
[ 1 3 5 7 9]
[[[[ 0 1]
[ 2 3]]
[[ 4 5]
[ 6 7]]]
[[[ 8 9]
[10 11]]
[[12 13]
[14 15]]]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[[0.702 0.396 0.133 0.241]
2
[0.403 0.825 0.911 0.304]
[0.705 0.565 0.329 0.681]] # Note: values vary every run
[-10 -7 -5 -3 -1 0 1 3 5 7]
🧮 Type & Size
c = [Link](-10, 10, 10, dtype=int)
print([Link]) # Bytes per element
print([Link](np.int32)) # Convert dtype
✅ Output:
4
[-10 -7 -5 -3 -1 0 1 3 5 7]
🔁 Iteration Example
for i in [Link](c):
print(i)
✅ Output:
-10
-7
-5
-3
-1
0
1
3
5
7
📚 Stacking Arrays
a1 = [Link](12).reshape(3, 4)
a2 = [Link](12, 24).reshape(3, 4)
3
print([Link]((a1, a2))) # Horizontal stack
print([Link]((a1, a2))) # Vertical stack
✅ Output:
[[ 0 1 2 3 12 13 14 15]
[ 4 5 6 7 16 17 18 19]
[ 8 9 10 11 20 21 22 23]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
📏 Normalization (Z-score)
✅ Purpose
Used to scale features: Mean = 0, Std Dev = 1
📌 Formula
normalized_data = (data - mean) / std_dev
💡 Example
arr = [Link]([1, 2, 3, 4, 5])
mean = [Link]()
std = [Link]()
normalized = (arr - mean) / std
print(normalized)
✅ Output:
[-1.26491106 -0.63245553 0. 0.63245553 1.26491106]
4
🚀 Broadcasting in NumPy
📖 What is Broadcasting?
Broadcasting describes how NumPy handles arrays with different shapes during arithmetic operations.
It lets you perform operations on arrays of different but compatible shapes without
copying data.
✅ Basic Example
a = [Link](6).reshape(2, 3)
b = [Link](3).reshape(1, 3)
print(a)
print(b)
print(a + b)
✅ Output:
[[0 1 2]
[3 4 5]]
[[0 1 2]]
[[ 0 2 4]
[ 3 5 7]]
📏 Broadcasting Rules
1. Align Dimensions: Add leading 1s to the shape of the smaller array.
2. Match Each Dimension:
3. Same size ✅
4. Size 1 gets stretched ✅
5. Different sizes ❌ Error
✅ More Valid Examples
a = [Link](12).reshape(4, 3)
b = [Link](3)
print(a)
print(b)
print(a + b)
5
✅ Output:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[0 1 2]
[[ 0 2 4]
[ 3 5 7]
[ 6 8 10]
[ 9 11 13]]
❌ Invalid Example
a = [Link](12).reshape(3, 4)
b = [Link](3)
print(a + b) # ValueError: operands could not be broadcast
🧠 Common Interview Questions
1. What is the difference between NumPy array and Python list?
2. What is broadcasting? Explain with example.
3. How does NumPy handle memory better than Python lists?
4. Give code to normalize a NumPy array.
5. Explain how you can reshape and stack arrays.
6. Why is NumPy used in data science & ML?
✅ Practice Tasks
1. Normalize a 1D array of 10 elements.
2. Create a 4D array with arange and reshape .
3. Stack two 3x4 matrices horizontally.
4. Predict output of a broadcasting operation.
5. Convert a float array to int using astype() .
📌 With this strong foundation in NumPy, you're well-prepared for any technical interview or
data science challenge!