Chapter 2: NumPy Quiz
NumPy Quiz written in the style of a kind but demanding teacher who really wants you to understand — not just memorize — NumPy.
I’ve divided it into 5 difficulty levels, from warm-up to expert.
Each question has:
- clear instructions
- expected output format (when applicable)
- hints (only look after trying!)
- full solution + explanation after each block
Instructions for you (the student):
- Try to solve every question without looking at the solution first
- Write your code in a notebook or editor
- Compare your result with the solution
- If you get stuck → read the hint
- If still stuck → look at the solution, but then immediately re-do the question yourself without copying
Ready? Let’s begin.
Level 1 – Warm-up (Basic creation & properties)
Q1. Create four different NumPy arrays that all contain exactly the numbers 0 through 9 (inclusive). Use four different creation methods.
Q2. Create a 6×8 array filled with the number 99. Then change its data type to int16. Finally print: shape, ndim, size, itemsize, nbytes, dtype.
Q3.
|
0 1 2 3 4 5 6 |
a = np.arange(24).reshape(4, 6) |
Without typing any numbers manually, print:
- the shape
- the number of bytes one element occupies
- the total memory used by the array in bytes
- the data type
Level 2 – Indexing & Slicing (very important)
Q4.
|
0 1 2 3 4 5 6 |
arr = np.arange(100).reshape(10, 10) |
Extract and show (print) the following sub-arrays:
a) the bottom-right 4×4 corner b) every second row and every second column (checkerboard style – starting from 0,0) c) the anti-diagonal (from top-right to bottom-left) d) all elements that are multiples of 11
Q5. Given the same arr:
Replace every number that is divisible by 7 with −999.
Then replace every number that is greater than 90 with 999.
Do both replacements in one single code block (you may use multiple lines).
Q6. Create a 10×10 array filled with zeros. Then set the border (first and last row + first and last column) to 1. Do it without loops and without hard-coding indices
Level 3 – Broadcasting & Arithmetic
Q7. Create a 12×12 multiplication table (1×1 to 12×12) using broadcasting only. Do not use np.meshgrid, np.outer, or any loops.
Q8.
|
0 1 2 3 4 5 6 |
X = np.random.randn(5000, 20) |
Normalize each column so that it has:
- mean = 0
- standard deviation = 1
Do it in one line.
Then normalize each row the same way (in one line).
Q9. Create a 100×100 array where:
- element [i,j] = sin(i) + cos(j)
- i and j are 0-based indices
Do it without loops and without meshgrid.
Level 4 – Reductions, Masks, Where, Unique
Q10.
|
0 1 2 3 4 5 6 |
data = np.random.randint(-50, 150, size=(200, 8)) |
Create a new array containing only the rows where:
- the row mean is between 40 and 60 (inclusive)
- AND at least 3 values are positive
Q11. Given the same data:
Replace all values outside the range [0, 100] with the nearest boundary (0 or 100).
Do it in one expression using np.clip or np.where.
Q12.
|
0 1 2 3 4 5 6 7 |
labels = np.random.randint(0, 5, size=10000) values = np.random.randn(10000) |
Compute the mean and standard deviation of values for each label 0–4.
Do it without loops and without pandas.
Q13.
|
0 1 2 3 4 5 6 |
arr = np.random.randint(0, 100, size=5000) |
Find the 10 most frequent values and their counts.
Show them sorted by frequency (descending).
Level 5 – Expert / Interview-style questions
Q14. Create a 10×10 array where the value at position [i,j] is the GCD of (i+1) and (j+1).
Q15. Given a 1D array of length 1_000_000 filled with random integers 1–100:
a) Find how many numbers appear exactly 10 000 times b) Find the 10 numbers that appear most frequently (with their counts)
Do both without loops and without pandas.
Q16. Create a function that takes a 2D array and returns a boolean mask of the same shape where:
- True = the element is a local maximum (greater than all its 4 direct neighbors — up, down, left, right)
- False = otherwise
Handle borders correctly (border cells have fewer neighbors).
Do not use loops.
Q17. Given a 1D array a of length N:
Create a 2D array of shape (N−4, 5) where each row is a sliding window of 5 consecutive elements from a.
Do it without loops (use only views / strides if possible).
Q18. (very hard) Given a 2D array of shape (m, n):
Replace every element with the median of its 3×3 neighborhood (including itself). Handle borders by padding with the nearest value (replicate padding).
Do not use loops or scipy.signal.
Now — try to solve at least 5–6 questions before looking at solutions.
When you’re ready, just say:
- “Show solutions for questions 1–10”
- “I want solutions only for the hard ones (14–18)”
- “Give me hints for 16–18 first”
- “Create even harder questions”
- or any other request
I’m here — let me know how you want to proceed! 😊
