Number Systems & Bases
Computers represent all data as binary (base 2). Understanding how numbers are encoded prevents bugs in scientific computing.
| Base | Name | Digits | Example () |
|---|---|---|---|
| 2 | Binary | 0, 1 | |
| 10 | Decimal | 0–9 | |
| 16 | Hexadecimal | 0–9, A–F |
Conversion formula: .
Basic Python types:
- int: exact integer (
42) - float: IEEE 754 double, digits (
3.14) - str: text (
"physics") - bool: True / False
Floats, Conversion & Pitfalls
Floating-point caution: Not all decimals are exact in binary representation.
>>> 0.1 + 0.2
0.30000000000000004
Common pitfall: Floats are not real numbers. Compare numerical results using a tolerance (abs(a - b) < 1e-9), never with ==, or your simulation will fail due to pure representation artifacts.
Type conversion:
int("42")converts str intfloat("3.14")converts str floatstr(99)converts int str
Physics link: Watch for integer division (5 // 2 = 2) versus float division (5 / 2 = 2.5) to avoid off-by-one errors.