E ExamMaster

Engineering Python · NumPy & pandas Basics

NumPy Ops and Slicing

In Engineering Python because labs scale features and take subsets — elementwise ops, slicing and aggregations replace slow Python loops.

Arithmetic on arrays is elementwise. Slicing selects views; aggregations like `mean` and `sum` collapse axes.

  • Engineering Python
  • Medium level
  • 4 concepts

1Elementwise arithmetic

`a + 1` and `a * b` operate elementwise when shapes align. This is the default mental model — not matrix multiply (`@`).

Figure. a = [1.0, 2.0, 3.0] times 2 scales each cell on its own: 2.0, 4.0, 6.0. That is elementwise multiply, not matrix multiply.

Elementwise

import numpy as np
a = np.array([1.0, 2.0, 3.0])
print(a * 2)
print(a + a)
What is `np.array([1,2]) * 2`?
  1. array([2, 4])
  2. array([1, 2, 1, 2])
  3. 4

Multiplication scales each element.

2Slicing arrays

`a[1:3]` and `X[:, 0]` select subsets. For 2-D, first index is rows, second is columns.

Figure. X is [[1, 2, 3], [4, 5, 6]]. X[:, 1] keeps every row and column index 1, so the slice is the middle column 2 and 5.

Takeaway

  1. Idea`a[1:3]` and `X[:, 0]` select subsets. For 2-D, first index is rows, second is columns.

Column slice

import numpy as np
X = np.array([[1, 2, 3], [4, 5, 6]])
print(X[:, 1])

3Aggregations

`a.mean()`, `a.sum()`, `a.max()` reduce an array to a summary. Pass `axis=` to reduce along rows or columns.

Figure. mean, sum and max collapse the whole array a to one summary each. Pass axis= to collapse along rows or columns instead.

Takeaway

  1. Idea`a.mean()`, `a.sum()`, `a.max()` reduce an array to a summary. Pass `axis=` to reduce along rows or columns.

4Lab: scale and mean

Scale an array and print its mean.

Figure. a is [1.0, 2.0, 3.0, 4.0]. Scale by 2, then mean: (2+4+6+8)/4 = 5.0, which is what (a * 2).mean() prints.

Takeaway

  1. IdeaScale an array and print its mean.

Coding lab. Scale and aggregate runs in the app, with checks on your output.

Notes

  • In Engineering Python because labs scale features and take subsets — elementwise ops, slicing and aggregations replace slow Python loops.
  • `a + 1` and `a * b` operate elementwise when shapes align. This is the default mental model — not matrix multiply (`@`).
  • `a[1:3]` and `X[:, 0]` select subsets. For 2-D, first index is rows, second is columns.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

Elementwise ops scale arrays; slices select; mean/sum aggregate.

Elementwise arithmetic
`a + 1` and `a * b` operate elementwise when shapes align. This is the default mental model — not matrix multiply (`@`).
Slicing arrays
`a[1:3]` and `X[:, 0]` select subsets. For 2-D, first index is rows, second is columns.
Aggregations
`a.mean()`, `a.sum()`, `a.max()` reduce an array to a summary. Pass `axis=` to reduce along rows or columns.
Lab: scale and mean
Scale an array and print its mean.

Practise NumPy Ops and Slicing

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 1 quick check with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.