Courses / Physics I
Computer Science

Lists, arrays, and structured storage

Physics I 233 words Free to read

Python Lists & NumPy Arrays

Physics simulations handle collections like particle positions or time series. Python provides lists while NumPy provides arrays.

Python lists are ordered, mutable, and can hold mixed types. They use 0-indexing: temperatures[0] gets the first item, and len() gives the count.

NumPy arrays are homogeneous, fast, and support vectorised operations. Functions like np.linspace generate points, and operations apply element-wise.

FeaturePython ListsNumPy Arrays
TypesMixed / HeterogeneousHomogeneous
SpeedInterpreted PythonCompiled C
MemoryObject overheadRaw compact data
Tip: Use np.array for numerical computation and Python list for heterogeneous or small collections.
The same "+1" takes five beats on a list and one beat on an array

Slicing, Speed & Pitfalls

Slicing extracts subsets using start:stop:step syntax. For example, data[1:4] returns indices 1, 2, and 3, while data[::2] takes every second element.

Broadcasting allows math operations like a * 2 to multiply every element without writing an explicit loop.

OperationSyntax ExampleResult
Basic Slicedata[1:4][20, 30, 40]
Strided Slicedata[::2][10, 30, 50]
Common pitfall: Assigning a list copies a reference, not the data. After b = a, mutating b mutates a too. Always use explicit copies like list(a) or slicing to fork data.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes

Computer Science