Programming and Computation · Module 8 of 10

Numerical Python with Arrays

Once you handle whole datasets, plain lists get slow and clumsy. NumPy gives you the array: apply a formula to a thousand values at once, with no loop, and summarise them in one call. This is the workhorse of engineering computation.

01

Readiness check

This module handles whole datasets at once. Tick only what you can do closed-notes.

  • Use lists and indexing from Module 7.
  • Recall element-wise operations on a set of numbers.
  • Recall a dot product or a weighted sum.
  • Recall evenly spaced points along an axis.
  • Recall importing a module from Module 6.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit data structures in Module 7.
3 or more weak itemsRevisit functions and imports in Module 6.
02

The core idea

A NumPy array holds many numbers and lets you compute on all of them at once. Arithmetic is element-wise, so a * 2 doubles every entry without a loop. Array methods like mean and sum summarise in one call, dot gives a weighted sum, and linspace builds evenly spaced values.

import numpy as npa = np.array([2, 4, 6, 8])a * 2 doubles every element

Lists are fine for a handful of values, but engineering data comes in hundreds or thousands, and NumPy is built for exactly that. You create an array with np.array([2, 4, 6, 8]), importing the library once as import numpy as np. The defining feature is vectorization: an operation applies to every element at once, so a * 2 is [4, 8, 12, 16] and a + b adds two arrays element by element, all without writing a loop. This is both faster and clearer than looping, because the code reads like the maths. Arrays carry summary methods, a.mean(), a.sum(), a.max(), that reduce a whole array to one number, so a mean is a single call. For linear-algebra style work, np.dot(a, b) forms the sum of products, the weighted sum or projection at the heart of many engineering formulas. And when you need a grid of points, say to evaluate a function along a beam, np.linspace(0, 10, 5) gives five evenly spaced values from 0 to 10. NumPy is the foundation every later numerical-methods, data, and simulation tool builds on, so getting comfortable with arrays now pays off through the rest of the roadmap.

The skill works when: you replace element-by-element loops with one vectorized array expression.
The skill breaks down when: arrays of different shapes are combined, or a loop is written where an array operation would do.
The concept. A vectorized operation applies to every array element at once, replacing an explicit loop with a single, readable expression.
03

The skills, taught in order

Five skills make array computation natural.

8.1 Creating arrays

import numpy as np, then np.array([2, 4, 6, 8]) makes an array from a list. np.zeros(n) and np.ones(n) make arrays of a given size to fill in.

8.2 Vectorized arithmetic

Operations apply element-wise: a * 2, a + b, a ** 2. No loop is needed, and the code reads like the mathematics it represents.

8.3 Array summaries

Reduce an array to one number with a.mean(), a.sum(), a.max(), and a.min(). These are the array versions of the built-ins from Module 7.

OperationExampleResult
Element-wisenp.array([2,4,6,8]) * 2[4, 8, 12, 16]
Meannp.array([2,4,6,8]).mean()5.0
Even spacingnp.linspace(0, 10, 5)[0, 2.5, 5, 7.5, 10]

A few array moves that replace many lines of loop code.

8.4 The dot product

np.dot(a, b) forms the sum of element-wise products, a weighted sum or projection: with [1,2,3] and [4,5,6] it is 4 + 10 + 18 = 32. This underlies much of engineering linear algebra.

8.5 Building grids with linspace

np.linspace(start, stop, n) returns n evenly spaced values including both ends. It is how you build the x-axis to evaluate a function along, ready for plotting (Module 9).

Engineering connection: evaluating a stress formula at 500 points along a part, or averaging a sensor array, is one vectorized line, which is why NumPy underlies every numerical-methods and simulation tool ahead.

04

Worked example 1: array arithmetic and mean

Double every element of an array and compute its mean and sum.

import numpy as np

a = np.array([2, 4, 6, 8])
print(a * 2)
print(a.mean(), a.sum())

Output:

[ 4  8 12 16]
5.0 20
  1. ProblemDouble an array and summarise it.
  2. Given / finda = [2, 4, 6, 8]. Find a*2, the mean, and the sum.
  3. ModelVectorized a * 2; reduce with mean and sum.
  4. Solvea * 2 = [4, 8, 12, 16]; mean = 5.0; sum = 20.
  5. CheckThe four values sum to 20 over 4 items, giving a mean of 5.0.
  6. ConclusionOne expression applies to every element, and one call summarises the whole array.
Result. [4, 8, 12, 16]; mean 5.0, sum 20.
05

Worked example 2: dot product and linspace

Compute a weighted sum with the dot product, and build five evenly spaced points from 0 to 10.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))
print(np.linspace(0, 10, 5))

Output:

32
[ 0.   2.5  5.   7.5 10. ]
  1. ProblemFind the dot product of two arrays and make an evenly spaced grid.
  2. Given / finda = [1,2,3], b = [4,5,6]. Find the dot product and linspace(0,10,5).
  3. Modelnp.dot sums the element-wise products; np.linspace spaces points.
  4. Solvedot = 1×4 + 2×5 + 3×6 = 32; linspace gives [0, 2.5, 5, 7.5, 10].
  5. Check4 + 10 + 18 = 32, and five points across a span of 10 are 2.5 apart.
  6. ConclusionThe dot product is the vectorized weighted sum, and linspace builds the axis for evaluating functions.
Result. dot = 32; [0, 2.5, 5, 7.5, 10].
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Looping over an arraySlow, verbose code"Can this be vectorized?"Use an array expression like a * 2.
Mismatched shapesA broadcasting or shape error"Do the arrays line up?"Combine arrays of compatible sizes.
List where array is needed[1,2] * 2 repeats, not doubles"Is this a list or an array?"Make it an array first with np.array.
Confusing dot with element-wiseWrong result for a weighted sum"Sum of products, or element-wise?"Use np.dot for the sum of products.
07

Practice ladder

Level 1 · Direct skill

Make the array [3, 6, 9] and compute its mean.

Show answer

np.array([3, 6, 9]).mean() is 6.0.

Level 2 · Mixed concept

Convert an array of forces in newtons to kilonewtons in one line.

Show answer

forces / 1000 divides every element, giving the kilonewton array with no loop.

Level 3 · Independent problem

Build 11 evenly spaced positions from 0 to 1 m and evaluate a stress that varies as position squared.

Show answer

x = np.linspace(0, 1, 11), then sigma = k * x ** 2 evaluates the whole array at once.

Transfer task | Real engineering

Take an array of sensor readings and report the mean, maximum, and standard deviation using array methods.

What good work looks like

data.mean(), data.max(), and data.std(), each printed with a label and units. A good answer uses array methods rather than manual loops.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain the difference between a list times 2 and an array times 2."
"Give me two arrays; I will predict the dot product, then run it."
"Vectorize all my code." Rewrite one loop as an array expression yourself.
"Why the shape error?" Check the array sizes yourself.

Portfolio task

Take a loop-based calculation from an earlier module and rewrite it as a single vectorized NumPy expression, checking the result matches.

Must include: a NumPy array, one vectorized operation, and a summary method, checked against the loop version.
09

Retrieval and spaced review

Closed notes. Answer out loud, then reveal.

1. How do you import NumPy?

import numpy as np.

2. What does vectorized mean?

An operation applies to every element at once, with no loop.

3. What does np.dot compute?

The sum of element-wise products, a weighted sum.

4. What does linspace give?

Evenly spaced values including both endpoints.

5. Why use arrays over lists for data?

Vectorized, fast, and readable for whole-dataset math.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRewrite one loop as a vectorized expression.
+3 daysEvaluate a formula over a linspace grid.
+7 daysMove on to plotting in Module 9.
+30 daysReach for arrays whenever you process a dataset.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
NumPy arrays and vectorizationFangohr, chapter on Numerical Python (numpy)
Array creation and operationsFangohr, Numerical Python, array basics
Why arrays for computationFangohr, Chapter 1, computational modelling

Chapter titles refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.