Tutorial

NumPy Foundations: 30 Questions That Build Array Intuition

Learn NumPy from zero — arrays vs lists, zeros, arange, reshape, broadcasting, and vectorised operations with 30 hands-on questions.

Anuj SainiAug 23, 20264 min read

NumPy is the engine under Pandas. This 30-question challenge builds array intuition — memory, shapes, broadcasting, and vectorisation — with every solution runnable inline.

What 30 questions cover and why that order?

Part 1 arrays vs lists -> zeros/ones/arange -> slicing -> reshape -> math -> broadcasting -> aggregate -> random -> linear algebra, each anchored to a YouTube reference (NumPy in 5 Minutes, Broadcasting Explained). See also Pandas fundamentals for the tabular layer above NumPy and Python basics workbook for the pre-step.

Ingredients: 30 graded prompts with import numpy as np pre-done; seed-free so outputs vary slightly — focus on shapes and dtypes, not values.

How do arrays differ from lists?

Setup:

python
import numpy as np
python
# Q1-Q3: List vs array
my_list = [1, 2, 3]
my_arr = np.array([1, 2, 3])
print(my_arr, my_arr.dtype)  # [1 2 3] int64
 
# Size
print(my_arr.itemsize)  # bytes per element (e.g., 8)
 
# Addition semantics
print([1,2] + [3,4])                          # [1, 2, 3, 4] concatenation
print(np.array([1,2]) + np.array([3,4]))      # [4 6] element-wise
 
# Q4-Q6: Constructors
print(np.zeros(5))              # [0. 0. 0. 0. 0.]
print(np.ones((2, 3)))          # 2x3 ones
print(np.linspace(0, 1, 5))     # [0.   0.25 0.5  0.75 1.  ]
print(np.arange(0, 10, 2))      # [0 2 4 6 8]

Rendered output: dtype int64, broadcasting difference visible in the addition pair — the most-tested interview contrast.

How do shapes, slicing, and reshape work?

python
arr = np.arange(12)
print(arr.reshape(3, 4))
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
 
print(arr[2:7])        # slice
print(arr[-3:])        # last 3
print(arr.reshape(3,4).T)  # transpose flips axes
 
# Filtering
print(arr[arr > 6])    # [ 7  8  9 10 11] boolean mask

Rendered output confirms row-major reshape; -3: selects tail without copying semantics you might expect.

How do broadcasting and vectorisation replace loops?

python
a = np.array([[1],[2],[3]])   # (3,1)
b = np.array([10,20,30,40])   # (4,)
print(a + b)  # (3,4) broadcast
# [[11 21 31 41]
#  [12 22 32 42]
#  [13 23 33 43]]
 
# Vectorised math vs loop
arr = np.arange(1, 6)
print(arr * 2)           # [ 2  4  6  8 10]
print(np.sqrt(arr))      # [1. 1.41 1.73 2. 2.23]
print(arr.mean(), arr.std())
Feature / Criteria

Gotcha: Integer Array Cannot Hold NaN

np.array([1,2,3], dtype=int) assigned np.nan becomes a cast error or a huge int sentinel — NaN is float-only. Use dtype=float when missing is possible, or mask with np.where.

What do you do after foundations?

Stack arrays into DataFrames per Pandas fundamentals and replace explicit for loops with df['col'].values plus NumPy ufuncs for the 10x speedup.


Download the Notebook and Practise

This article is a walkthrough of a runnable Jupyter notebook. Download the original .ipynb and run it locally or on Colab — every code block above appears in order.

Download the Numpy Foundations Notebook

Get the complete .ipynb with outputs — runs on any Python 3.10+ environment with pandas, numpy, and the libraries listed in setup.

Download .ipynb

Continue your track: Data Analyst Roadmap · Python and Pandas Guide · SQL NULL Handbook · SQL JOIN Fan-Out · Topfolio Practice · Data Analyst vs Engineer

Dataset generators where applicable are in courses/workbooks/generators/ — see citations atop for the exact *.py source for this notebook.


Frequently Asked Questions

Why is NumPy faster than Python lists?

NumPy stores typed, contiguous arrays and pushes loops into C. A vectorised arr*2 replaces a Python for-loop and cuts runtime by 10-100x.

What is broadcasting in NumPy?

Rules that let differently shaped arrays operate element-wise: a (3,1) plus (1,4) broadcasts to (3,4) without copying. Read as 'stretch the smaller shape to match.'

When do you use reshape vs transpose?

reshape changes shape (and may copy) while transpose swaps axes. arr.reshape(2,6) reorders elements row-major; arr.T flips rows and columns.

How do you avoid silent dtype bugs?

Check arr.dtype and use np.nan for missing floats — integer arrays cannot hold NaN. Cast explicitly with arr.astype(float).

Frequently Asked Questions

Why is NumPy faster than Python lists?

NumPy stores typed, contiguous arrays and pushes loops into C. A vectorised arr*2 replaces a Python for-loop and cuts runtime by 10-100x.

What is broadcasting in NumPy?

Rules that let differently shaped arrays operate element-wise: a (3,1) plus (1,4) broadcasts to (3,4) without copying. Read as 'stretch the smaller shape to match.'

When do you use reshape vs transpose?

reshape changes shape (and may copy) while transpose swaps axes. arr.reshape(2,6) reorders elements row-major; arr.T flips rows and columns.

How do you avoid silent dtype bugs?

Check arr.dtype and use np.nan for missing floats — integer arrays cannot hold NaN. Cast explicitly with arr.astype(float).

Anuj Saini

Written by

Anuj SainiFounder & Lead Instructor

Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.