Python Tutorial / Understand Integers, Floats and Numeric Conversion
Python int represents integers, while float represents binary floating-point numbers. For amounts requiring exact decimal rules, use integer units or Decimal and make the unit and rounding policy explicit.
Read arithmetic operators precisely
| Expression | Operation | Result |
|---|---|---|
| 7 / 2 | Division | 3.5 |
| 7 // 2 | Floor division | 3 |
| 7 % 2 | Remainder | 1 |
| 2 ** 3 | Exponentiation | 8 |
Floor division rounds toward negative infinity, rather than truncating toward zero. Thus -7 // 2 is -4, whereas int(-3.5) is -3. Use parentheses when operator precedence would obscure your intent. Python integers are not limited to a fixed 32-bit range, although memory is finite.
For the relevant APIs and platform requirements, see Python: floatingpoint.
Amount types and later analysis build on the rules demonstrated in Clean and Analyze Tabular Data with pandas.
Run a comparison of numeric types
Save the complete code below as numeric_demo.py in a separate practice folder, using UTF-8. Run python numeric_demo.py in a terminal with your project virtual environment selected. Outside a virtual environment on macOS or Ubuntu, use python3 when that is your interpreter command.
from decimal import Decimal
print(7 / 2, 7 // 2, 7 % 2)
print(-7 // 2, int(-3.5))
print(0.1 + 0.2 == 0.3)
print(Decimal("0.1") + Decimal("0.2"))
price_won = 3500
print(price_won * 3)
Expected output:
3.5 3 1 -4 -3 False 0.3 10500
Choose conversion and precision rules
int("3500") parses an integer string. int("3.5") fails, while int(3.5) truncates the fractional part of a float. float("3.5") creates a floating-point value. Formatting a value to fewer displayed decimal places does not make its stored representation exact.
Decimal supports decimal arithmetic. Construct it from a decimal string, such as Decimal("0.1"), to avoid carrying over a float’s representation error. Precision and rounding still require an explicit policy appropriate to the calculation.
Formatting a float to two decimal places changes its display, not its stored value. format(0.1 + 0.2, ".2f") gives the string "0.30" without turning the calculation into exact decimal arithmetic. Use integer minor units when appropriate, or construct Decimal values from strings and specify rounding rules when exact decimal accounting is required.
Check monetary boundaries
Whole-won expenses can be stored as integers such as 3500. Other currencies or fractional units need documented units and rounding rules. A numeric type alone cannot decide business policy.
- Compare -7 // 2 with -7 / 2 and explain the difference.
- Change the divisor to zero to observe ZeroDivisionError, then restore two.
- Try converting "3500 KRW" with int() and note why input validation is needed.
Use the input conversion example to locate the boundary between user text and numeric data. Decide which representations are accepted before silently stripping meaningful characters.




