Computers represent all data as binary (base 2). Understanding how numbers and text are encoded prevents subtle bugs in scientific computing.
Number systems
| Base | Name | Digits | Example () |
|---|---|---|---|
| 2 | Binary | 0, 1 | |
| 10 | Decimal | 0–9 | |
| 16 | Hexadecimal | 0–9, A–F |
Conversion: .
Basic Python types
x = 42 # int — exact integer
y = 3.14 # float — IEEE 754 double (≈15 decimal digits)
s = "physics" # str — text
b = True # bool — True / False
Floating-point caution — Not all decimals are exact in binary:
>>> 0.1 + 0.2
0.30000000000000004
For scientific work, compare floats with a tolerance: abs(a - b) < 1e-9.
Type conversion
n = int("42") # str → int
x = float("3.14") # str → float
s = str(99) # int → str
Physics link: When a simulation gives unexpected results, check for integer division (5 // 2 = 2) vs float division (5 / 2 = 2.5). This is a classic source of off-by-one physics errors.
Common pitfall: Floats are not real numbers: 0.1 + 0.2 ≠ 0.3 exactly. Compare numerical results with a tolerance, never with ==, or your simulation will "fail" for reasons that are pure representation.