Courses / Physics I
Computer Science

Data representation and basic types

Physics I 240 words Free to read

Computers represent all data as binary (base 2). Understanding how numbers and text are encoded prevents subtle bugs in scientific computing.

Number systems

BaseNameDigitsExample (131013_{10})
2Binary0, 1110121101_2
10Decimal0–9131013_{10}
16Hexadecimal0–9, A–FD16\text{D}_{16}

Conversion: 11012=1 ⁣ ⁣23+1 ⁣ ⁣22+0 ⁣ ⁣21+1 ⁣ ⁣20=13101101_2 = 1\!\cdot\!2^{3} + 1\!\cdot\!2^{2} + 0\!\cdot\!2^{1} + 1\!\cdot\!2^{0} = 13_{10}.

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.

Practise this lesson

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

13practice questions
2interactive scenes
Start Physics I free

Computer Science