# Data representation and basic types

Physics I · Computer Science · https://tryals.app/learn/physics-i/data-representation-and-basic-types

## 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 ($13_{10}$) |
| --- | --- | --- | --- |
| 2 | Binary | 0, 1 | $1101_2$ |
| 10 | Decimal | 0–9 | $13_{10}$ |
| 16 | Hexadecimal | 0–9, A–F | $\text{D}_{16}$ |

**Conversion formula**:
$1101_2 = 1\cdot2^{3} + 1\cdot2^{2} + 0\cdot2^{1} + 1\cdot2^{0} = 13_{10}$.

**Basic Python types**:
- **int**: exact integer (`42`)
- **float**: IEEE 754 double, $\approx15$ digits (`3.14`)
- **str**: text (`"physics"`)
- **bool**: True / False

## Floats, Conversion & Pitfalls

**Floating-point caution**: Not all decimals are exact in binary representation.

```python
>>> 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 $\to$ int
- `float("3.14")` converts str $\to$ float
- `str(99)` converts int $\to$ str

**Physics link**: Watch for **integer division** (`5 // 2 = 2`) versus **float division** (`5 / 2 = 2.5`) to avoid off-by-one errors.

## Practice questions

5 of this lesson's 13 practice questions, with answers. The full set is in the app.

### 1. Order the steps to convert the decimal number 13 to binary.

**Answer:**

1. Divide 13 by 2: quotient 6, remainder 1
2. Divide 6 by 2: quotient 3, remainder 0
3. Divide 3 by 2: quotient 1, remainder 1
4. Divide 1 by 2: quotient 0, remainder 1 → read remainders bottom-up: 1101

**Why:** Repeated division by 2, reading remainders bottom-up, gives 13₁₀ = 1101₂.

Page: https://tryals.app/practice/physics-i/data-representation-and-basic-types/order-the-steps-to-convert-the-decimal-number-13-to-binary

### 2. In Python, `0.1 + 0.2 == 0.3` is **False**. What is the root cause?

A. 0.1 and 0.2 have no exact binary representation
B. The == operator is unreliable for numbers
C. A well-known bug in Python’s addition
D. Computers drop the last decimal digit of floats

**Answer:** A. 0.1 and 0.2 have no exact binary representation

**Why:** Binary floats can only finitely represent sums of powers of 1/2; one-tenth repeats forever, so it is stored rounded. The sum lands at 0.30000000000000004. That is why numerical code compares with a tolerance (`abs(a-b) < 1e-9`), a lesson every physics simulation learns eventually.

Page: https://tryals.app/practice/physics-i/data-representation-and-basic-types/in-python-0-1-0-2-0-3-is-false-what-is-the-root-cause

### 3. A sensor stores readings as **8-bit** unsigned integers. How many distinct values can one reading take? **Set the slider to your answer.**

**Answer:** 256 (within ±7.68)

**Why:** $2^{8} = 256$ values (0–255). Every representation budget works this way: n bits → $2^{n}$ patterns. It is why 8-bit images have 256 gray levels and why your lab ADC clips at its bit depth.

Page: https://tryals.app/practice/physics-i/data-representation-and-basic-types/a-sensor-stores-readings-as-8-bit-unsigned-integers-how-many

### 4. In Python, the string '42' and the integer 42 are the same type.

**Answer:** False

**Why:** '42' is a str; 42 is an int. They must be converted to compare numerically.

Page: https://tryals.app/practice/physics-i/data-representation-and-basic-types/in-python-the-string-42-and-the-integer-42-are-the-same-type

### 5. In floating point, `0.1 + 0.2 == 0.3` is false. What does that reveal about how numbers are stored?

A. That the comparison operator is unreliable for floating-point values
B. That addition is implemented incorrectly for small numbers
C. That the result exceeds the available precision only when values are added
D. That values are held in binary with finite precision

**Answer:** D. That values are held in binary with finite precision

**Why:** Base 10 fractions are not generally base 2 fractions. This is why numerical code compares within a tolerance rather than for equality.

Page: https://tryals.app/practice/physics-i/data-representation-and-basic-types/in-floating-point-0-1-0-2-0-3-is-false-what-does-that-reveal
