An algorithm is a finite sequence of unambiguous steps that solves a problem. In physics, algorithms automate calculations from numerical integration to Monte Carlo simulations.
Variables and assignment
mass = 5.0 # kilograms
velocity = 3.0 # m/s
kinetic_energy = 0.5 * mass * velocity**2
A variable stores a value in RAM (main memory) and can be updated at any time.
Control flow structures
Conditional — Execute code only when a condition is true:
if temperature > 100:
print("Boiling!")
elif temperature > 0:
print("Liquid")
else:
print("Frozen")
Loop — Repeat a block of code:
# Euler method: simple numerical integration
x, v, dt = 0.0, 10.0, 0.01
for step in range(1000):
x += v * dt
v += -9.8 * dt # gravity
Key hardware context
- The processor (CPU) executes instructions one at a time (or in parallel on multiple cores).
- Main memory (RAM) holds data during execution — fast but volatile.
- Secondary storage (SSD/HDD) persists data after power-off.
Tip: Always initialise variables before using them. An uninitialised variable is the most common source of bugs in scientific code.
Common pitfall: The computer does exactly what the code says, never what you meant. Off-by-one boundaries in loops and conditions are where the gap between intention and instruction bites hardest.