|
| 1 | +--- |
| 2 | +sidebar_position: 5 |
| 3 | +--- |
| 4 | + |
| 5 | +# Numeric Representation |
| 6 | + |
| 7 | +Python supports different ways of representing numbers, including integer and real (floating point) numbers. |
| 8 | + |
| 9 | +## Integer Types (int) |
| 10 | + |
| 11 | +Integers in Python can be represented in various numerical systems: |
| 12 | + |
| 13 | +- **Decimal Notation (Base 10):** |
| 14 | + |
| 15 | + - This is the default way we represent numbers, such as `3` or `-89`. |
| 16 | + |
| 17 | +- **Binary Notation (Base 2):** |
| 18 | + |
| 19 | + - Represented using a prefix of `0b`. It consists only of `0` and `1`. |
| 20 | + - Example: `0b1011` is equivalent to `11` in decimal. |
| 21 | + |
| 22 | +- **Octal Notation (Base 8):** |
| 23 | + |
| 24 | + - Represented using a prefix of `0o`. It uses digits from `0` to `7`. |
| 25 | + - Example: `0o12` is equivalent to `10` in decimal. |
| 26 | + |
| 27 | +- **Hexadecimal Notation (Base 16):** |
| 28 | + - Represented using a prefix of `0x`. It uses digits from `0` to `9` and letters `A` to `F` to represent values `10` to `15`. |
| 29 | + - Example: `0x2B` is equivalent to `43` in decimal. |
| 30 | + |
| 31 | +### Conversion between Notations |
| 32 | + |
| 33 | +- To **convert binary to decimal**, sum each digit multiplied by `2` raised to the power of its position, starting from `0` on the right. |
| 34 | + |
| 35 | + - Example: `0b1011` in decimal is calculated as: |
| 36 | + - \( 1 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = 8 + 0 + 2 + 1 = 11 \) |
| 37 | + |
| 38 | +- To **convert octal to decimal**, use powers of `8`. |
| 39 | + |
| 40 | + - Example: `0o52` in decimal is calculated as: |
| 41 | + - \( 5 \times 8^1 + 2 \times 8^0 = 40 + 2 = 42 \) |
| 42 | + |
| 43 | +- To **convert hexadecimal to decimal**, use powers of `16`. |
| 44 | + - Example: `0x2B` in decimal is calculated as: |
| 45 | + - \( 2 \times 16^1 + 11 \times 16^0 = 32 + 11 = 43 \) |
| 46 | + |
| 47 | +Python can perform these conversions using built-in functions: |
| 48 | + |
| 49 | +- `bin(value)` returns the binary representation as a string. |
| 50 | +- `oct(value)` returns the octal representation as a string. |
| 51 | +- `hex(value)` returns the hexadecimal representation as a string. |
| 52 | + |
| 53 | +To **convert a string representation** back to an integer, use the `int()` function. |
| 54 | + |
| 55 | +- Example: `int("0b1011", 2)` converts the binary string to decimal. |
| 56 | + |
| 57 | +### Integer Variables in Different Notations |
| 58 | + |
| 59 | +When working with different notations, Python internally treats the value as an integer regardless of how it is expressed. |
| 60 | + |
| 61 | +- Example: |
| 62 | + ```python title="numbers.py" |
| 63 | + a = 10 |
| 64 | + b = 0b1010 |
| 65 | + c = 0o12 |
| 66 | + d = 0xA |
| 67 | + print(a, b, c, d) # Outputs: 10 10 10 10 |
| 68 | + ``` |
| 69 | + All four variables have the same value, `10`, but are written in different notations. |
| 70 | + |
| 71 | +## Floating-Point Numbers (float) |
| 72 | + |
| 73 | +Floating-point numbers represent real numbers and can have a fractional part. |
| 74 | + |
| 75 | +- **Standard Notation:** |
| 76 | + |
| 77 | + - Examples: `3.0`, `-89.14`. |
| 78 | + |
| 79 | +- **Scientific Notation:** |
| 80 | + - Represented using `e` or `E` to indicate powers of `10`. |
| 81 | + - Example: `1.5e3` is equivalent to \( 1.5 \times 10^3 = 1500 \). |
| 82 | + |
| 83 | +### Precision Issues with Floating-Point Numbers |
| 84 | + |
| 85 | +- Floating-point numbers may have **precision issues** because they cannot precisely represent all real numbers due to limitations in memory representation. |
| 86 | +- Example: |
| 87 | + ```python title="numbers.py" |
| 88 | + a = 0.1 |
| 89 | + b = 0.2 |
| 90 | + sum = a + b |
| 91 | + print("The sum is", sum) # Outputs: The sum is 0.30000000000000004 |
| 92 | + ``` |
| 93 | + Instead of `0.3`, Python returns a value close to `0.3` due to rounding errors in binary representation. |
| 94 | + |
| 95 | +### Addressing Precision Issues |
| 96 | + |
| 97 | +- For cases where precision is critical (e.g., financial calculations), Python provides the `decimal` module, which offers more accurate representation and operations. This module is beyond the scope of the video. |
| 98 | + |
| 99 | +## Practical Examples |
| 100 | + |
| 101 | +- **Printing Variables in Different Notations:** |
| 102 | + |
| 103 | + ```python title="numbers.py" |
| 104 | + a = 10 |
| 105 | + b = 0b1010 |
| 106 | + c = 0o12 |
| 107 | + d = 0xA |
| 108 | + print(bin(a), oct(b), hex(c), hex(d)) # Outputs: 0b1010 0o12 0xa 0xa |
| 109 | + ``` |
| 110 | + |
| 111 | + This example prints the binary, octal, and hexadecimal representations of the value `10`. |
| 112 | + |
| 113 | +- **Floating-Point Precision Example:** |
| 114 | + ```python title="numbers.py" |
| 115 | + a = 0.1 |
| 116 | + b = 0.2 |
| 117 | + sum = a + b |
| 118 | + print(f"The sum is approximately: {sum:.2f}") # Outputs: The sum is approximately: 0.30 |
| 119 | + ``` |
| 120 | + By formatting the output, we can display the sum as `0.30`, but the underlying value still contains precision errors. |
0 commit comments