Quiz/Exercises
Quiz + Exercises section on NumPy — written as if we are in a classroom together, I am your instructor, and I want you to really understand and remember the material.
I will first give you the exercises grouped by topic, then detailed solutions with explanations, then additional challenge questions for those who want to go further.
Feel free to pause after each question, try to solve it yourself (on paper or in a notebook), and only then look at the solution.
NumPy Quiz / Exercises – Full Practice Set
Section 1 – Basic Array Creation & Properties (Easy–Medium)
- Create a 1D array containing numbers from 0 to 19 (inclusive) using three different NumPy functions. Show the code and the resulting array for each method.
- Create a 5×5 identity matrix, then change all elements on the main diagonal to 7. Show the final matrix.
- Create a 4×6 array filled with the value 3.14 (use float32 dtype). Then print its shape, ndim, size, and dtype.
- Given this array:
Python0123456arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
Print:
- number of dimensions
- shape
- total number of elements
- data type
- Create a 1D array of 12 evenly spaced numbers between 0 and 5 (inclusive). Then reshape it into a 3×4 matrix.
Section 2 – Indexing & Slicing (Medium)
- Given:
Python0123456a = np.arange(0, 36).reshape(6, 6)
Extract and print: a) the third row (0-based) b) the last column c) the 3×3 submatrix from rows 2–4 and columns 1–3 (inclusive) d) every second element starting from index 5 (in the flattened view)
- Given the same array a: Replace all values that are divisible by 7 with -1.
- Using boolean indexing on the same array a, create a 1D array containing only the even numbers from it.
- Given:
Python0123456mat = np.random.randint(0, 100, size=(5, 8))
Create a new array containing only the rows where the sum of the row is greater than 300.
Section 3 – Arithmetic & Broadcasting (Medium–Hard)
- Given two arrays:
Python01234567A = np.arange(1, 13).reshape(3, 4)B = np.array([10, 20, 30, 40])
Compute A + B without writing any loops.
- Create a 10×10 multiplication table (1×1 up to 10×10) using broadcasting.
- Given a 1000×3 array of random 3D points:
Python0123456points = np.random.rand(1000, 3) * 100
Compute the Euclidean distance from each point to the origin (0,0,0) using broadcasting and vectorized operations.
Section 4 – Summation, Products, Differences (Medium–Hard)
- Given a 6×5 array of random integers between 1 and 100: a) Compute the sum of each row b) Compute the product of each column c) Compute the cumulative sum along each row
- Create an array of the first 15 positive integers. Compute the cumulative product. What is the last value?
- Given a time series:
Python0123456prices = np.array([100, 102, 98, 105, 103, 110, 108])
Compute the first differences and percentage changes.
Section 5 – Mixed / Challenge Questions
- Create a 7×7 checkerboard pattern (0 and 1 alternating) using broadcasting and arithmetic.
- Given a 1D array of 100 random numbers between 0 and 1: Count how many are greater than 0.7 without using a loop.
- Create a 10×10 array where each element (i,j) is the GCD of i+1 and j+1 (indices starting from 0).
- Given a 1D array of 1000 random integers between 1 and 100: Find the 10 most frequent numbers (and their counts) using np.unique(…, return_counts=True).
- Harder challenge Given a 2D array of shape (m, n), create a new array where each row is replaced by the cumulative product of that row.
Solutions & Detailed Explanations
Section 1 – Basic Array Creation & Properties
- Three ways to create 0–19:
|
0 1 2 3 4 5 6 7 8 |
print(np.arange(20)) print(np.linspace(0, 19, 20, dtype=int)) print(np.array(range(20))) |
- Identity matrix with diagonal = 7
|
0 1 2 3 4 5 6 7 8 9 10 |
eye = np.eye(5, dtype=int) eye[np.diag_indices(5)] = 7 print(eye) # or more elegant: eye = np.eye(5, dtype=int) * 7 + np.eye(5, dtype=int) * (1 - np.eye(5, dtype=int)) |
(But the first way is clearer)
- Filled array
|
0 1 2 3 4 5 6 7 |
arr = np.full((4, 6), 3.14, dtype=np.float32) print(arr.shape, arr.ndim, arr.size, arr.dtype) |
- Properties
|
0 1 2 3 4 5 6 7 8 9 |
print(arr.ndim) # 2 print(arr.shape) # (3, 4) print(arr.size) # 12 print(arr.dtype) # int64 (or int32 depending on system) |
- Evenly spaced + reshape
|
0 1 2 3 4 5 6 7 |
lin = np.linspace(0, 5, 12) mat = lin.reshape(3, 4) |
(continues in next message due to length)
