🐍 Python for Everyone

← Back to exercises

Chapter 23 Practice Bank 70-90 min, hands-on

Practice Bank: NumPy for Data Analysis

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Every task here uses real NumPy directly. No import beyond numpy/json/os/re/math/datetime/random/csv/threading/concurrent.futures.

How to run these Download starter.py below and run python3 starter.py from inside the same folder. Install NumPy first if you haven't: pip install numpy.

Topic 1: What NumPy Is & Why It Exists

1 Sum a list the loop way

Find # TODO 1.1. Write loop_sum(values).

2 Sum a list the NumPy way

Find # TODO 1.2. Write array_sum(values).

3 Debug the Code

Find # TODO 1.3. This wrongly explains NumPy's speed as being about memory usage. Fix it.

💼 Scenario

Comparing Two Approaches

Find # TODO 1.A. Write should_use_numpy(dataset_size, operation_count).

💼 Interview Prep

Why NumPy is foundational

Find # TODO 1.B. Write explain_why_numpy_is_foundational().

Topic 2: Creating Arrays

1 Build a range array

Find # TODO 2.1. Write build_range_array(start, stop, step).

2 Describe an array

Find # TODO 2.2. Write describe_array(arr).

3 Debug the Code

Find # TODO 2.3. This uses np.arange() where np.linspace() was needed. Fix it.

💼 Scenario

Sensor Baseline Array

Find # TODO 2.A. Write build_baseline_readings(count).

💼 Interview Prep

shape vs. dtype vs. ndim

Find # TODO 2.B. Write explain_shape_vs_dtype_vs_ndim().

Topic 3: Indexing, Slicing & Boolean Masking

1 Read a column

Find # TODO 3.1. Write get_column(grid, col_index).

2 Filter above a threshold

Find # TODO 3.2. Write above_threshold(arr, threshold).

3 Debug the Code

Find # TODO 3.3. This reads a column when a row was asked for. Fix it.

💼 Scenario

Flagging Failing Grades

Find # TODO 3.A. Write flag_failing_grades(scores, passing=60).

💼 Interview Prep

What a boolean mask is

Find # TODO 3.B. Write explain_boolean_masking().

Topic 4: Vectorized Math & Broadcasting

1 Scale an array

Find # TODO 4.1. Write scale_array(arr, factor).

2 Broadcast a row offset

Find # TODO 4.2. Write add_row_offset(grid, offsets).

3 Debug the Code

Find # TODO 4.3. This was supposed to demonstrate a broadcasting error but uses compatible shapes instead. Fix it.

💼 Scenario

Applying a Uniform Price Increase

Find # TODO 4.A. Write apply_price_increase(prices, percent).

💼 Interview Prep

The broadcasting compatibility rule

Find # TODO 4.B. Write explain_broadcasting_rule().

Topic 5: Aggregate Functions & axis=

1 Grand total

Find # TODO 5.1. Write grid_total(grid).

2 Column totals

Find # TODO 5.2. Write column_totals(grid).

3 Debug the Code

Find # TODO 5.3. This uses the wrong axis for per-row totals. Fix it.

💼 Scenario

Class Performance Report

Find # TODO 5.A. Write subject_and_student_averages(grades_grid).

💼 Interview Prep

What axis=0 and axis=1 collapse

Find # TODO 5.B. Write explain_axis_parameter().

Topic 6: Reshaping & Stacking

1 Reshape to a grid

Find # TODO 6.1. Write flat_to_grid(flat_array, rows, cols).

2 Flatten back to 1D

Find # TODO 6.2. Write grid_to_flat(grid).

3 Debug the Code

Find # TODO 6.3. This uses np.hstack() where np.vstack() was needed. Fix it.

💼 Scenario

Reshaping a Week of Sensor Data

Find # TODO 6.A. Write daily_averages(hourly_readings).

💼 Interview Prep

Reshape vs. stack

Find # TODO 6.B. Write explain_reshape_vs_stack().

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match on the explanation-style tasks — the goal is that your program runs without errors and does what each TODO asks. Every numeric task's output is exactly reproducible.
← Exercises Challenges →