
Quick answer: NumPy is the core Python library for fast numerical computing. It provides the ndarray – an n-dimensional array that stores numbers of one type – plus vectorised maths (no loops needed), statistics, linear algebra and random numbers. Install it with pip install numpy and import it as np. Engineers use NumPy for stress and load calculations, matrix methods, signal processing and handling simulation data.
All code examples were run on Python 3.13 with NumPy 2.5; the outputs shown are real.
Contents
- Install and Import NumPy
- Creating NumPy Arrays
- Vectorised Operations (No Loops)
- Indexing, Slicing and Boolean Filtering
- Reshape and Transpose
- Broadcasting
- Statistics with NumPy
- Matrix Multiplication vs Element-wise Multiplication
- Solving Linear Equations (Engineering Use)
- NumPy Array vs Python List
Install and Import NumPy
Install once with pip install numpy, then import it with the standard alias:
import numpy as np
Creating NumPy Arrays
Use np.array() for your own data, np.zeros() and np.ones() for filled arrays, np.arange() for steps and np.linspace() for evenly spaced points.
import numpy as np
a = np.array([10, 20, 30, 40])
print(a, a.dtype, a.shape)
print(np.zeros(3), np.ones((2, 2)))
print(np.arange(0, 10, 2), np.linspace(0, 1, 5))Output:
[10 20 30 40] int64 (4,) [0. 0. 0.] [[1. 1.] [1. 1.]] [0 2 4 6 8] [0. 0.25 0.5 0.75 1. ]
Vectorised Operations (No Loops)
Operations apply to every element at once. Here, stress is calculated for three loads in one line:
import numpy as np
force = np.array([1000, 2000, 3000]) # N
area = 50e-6 # m^2
stress = force / area / 1e6 # MPa
print(stress)
print(stress.max(), stress.mean())Output:
[20. 40. 60.] 60.0 40.0
Indexing, Slicing and Boolean Filtering
Use [row, column] for 2D arrays, : for whole rows or columns, and a condition inside brackets to filter values.
import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m[1, 2], m[:, 0], m[0:2, 1:])
print(m[m > 5])Output:
6 [1 4 7] [[2 3] [5 6]] [6 7 8 9]
Reshape and Transpose
import numpy as np
a = np.arange(6)
print(a.reshape(2, 3))
print(a.reshape(2, 3).T)Output:
[[0 1 2] [3 4 5]] [[0 3] [1 4] [2 5]]
Broadcasting
Broadcasting lets arrays of different shapes work together – for example converting units or building a table in one step.
import numpy as np
lengths_mm = np.array([100, 250, 400])
print(lengths_mm / 1000) # mm to m
print(lengths_mm[:, None] * np.array([1, 2]))Output:
[0.1 0.25 0.4 ] [[100 200] [250 500] [400 800]]
Statistics with NumPy
import numpy as np
temps = np.array([31.5, 33.0, 29.8, 35.2, 32.1])
print(round(temps.mean(), 2), temps.min(), temps.max(), round(temps.std(), 3))Output:
32.32 29.8 35.2 1.779
Matrix Multiplication vs Element-wise Multiplication
@ performs matrix multiplication; * multiplies element by element.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B)
print(A * B)Output:
[[19 22] [43 50]] [[ 5 12] [21 32]]
Solving Linear Equations (Engineering Use)
np.linalg.solve() solves systems of equations – used in truss analysis, circuit analysis and finite element methods.
import numpy as np
# Solve 2x + y = 5 and x + 3y = 10
A = np.array([[2, 1], [1, 3]])
b = np.array([5, 10])
print(np.linalg.solve(A, b))Output:
[1. 3.]
So x = 1 and y = 3.
NumPy Array vs Python List
| Feature | Python list | NumPy array |
|---|---|---|
| Data types | Mixed | Single type |
| Speed for maths | Slow (loops) | Fast (vectorised C code) |
| Memory | More | Less |
| Maths operators | + joins lists | + adds element-wise |
| Multi-dimensional | Nested lists | Native n-dimensional |
Frequently Asked Questions
What is NumPy in Python?
NumPy is a Python library for numerical computing that provides fast n-dimensional arrays, vectorised mathematical operations, statistics and linear algebra functions.
Why is NumPy faster than Python lists?
NumPy arrays store one data type in contiguous memory and run operations in optimised C code, so they avoid slow Python loops.
How do I install NumPy?
Run pip install numpy in the terminal, then use import numpy as np in your code.
What is the difference between @ and * in NumPy?
@ performs matrix multiplication, while * multiplies corresponding elements of two arrays.
Is NumPy useful for mechanical engineers?
Yes. It is used for load and stress calculations on many cases at once, solving equations, matrix-based methods, vibration and signal analysis, and processing simulation data.
Related Python Guides
- Pandas tutorial with Matplotlib
- Python programs with output
- Python for mechanical engineers
- Python roadmap
- All Python tutorials
Learn Python With Guidance
Join our Python course or message us on WhatsApp for doubt-clearing and project support.