Physics simulations deal with collections of data: time series, particle positions, field values. Python provides lists and NumPy provides arrays.
Python lists — Ordered, mutable, can hold mixed types:
temperatures = [20.1, 21.3, 19.8, 22.0]
temperatures.append(23.5) # add element
print(temperatures[0]) # → 20.1 (0-indexed)
print(len(temperatures)) # → 5
NumPy arrays — Homogeneous, fast, support vectorised operations:
import numpy as np
t = np.linspace(0, 10, 1000) # 1000 points from 0 to 10
x = np.sin(2 * np.pi * t) # element-wise sine
mean_x = np.mean(x) # statistical operations
Why arrays over lists?
- Speed: NumPy operations run in compiled C, not interpreted Python.
- Broadcasting:
a * 2multiplies every element — no loop needed. - Memory: Arrays store raw numbers; lists store Python objects with overhead.
Slicing
data = np.array([10, 20, 30, 40, 50])
data[1:4] # → [20, 30, 40] (indices 1, 2, 3)
data[::2] # → [10, 30, 50] (every 2nd element)
Tip: Usenp.arrayfor numerical computation and Pythonlistfor heterogeneous or small collections. Never loop over array elements when a vectorised operation exists.
Common pitfall: Assigning a list copies a reference, not the data: afterb = a, mutatingbmutatesatoo. Deliberate copies (list(a), slicing) are how you actually fork data.