Python Numbers
Python · Variables & Data Types
Theory
Python has three numeric types. int holds whole numbers of any size; there is no overflow, so 2 ** 100 is computed exactly. float holds decimal numbers using 64-bit binary floating point, which gives about 15 to 17 significant digits but cannot store most decimals exactly, so 0.1 + 0.2 shows 0.30000000000000004. complex holds numbers like 3+4j for scientific work.
Mixing types promotes the result: int with float gives a float, so 19.99 * 3 is 59.97. The / operator always returns a float (10 / 2 is 5.0), while // performs floor division, rounding down toward negative infinity, so -7 // 2 is -4, not -3. Underscores can make big literals readable: 1_000_000. For money where every paisa matters, the decimal module avoids binary rounding errors.
Why we use it
Numbers power every calculation in analysis: totals, averages, growth rates and percentages. Knowing how int and float behave stops subtle bugs such as an unexpected .0, a float equality check that fails, or a negative floor division that rounds the wrong way.
When to use it
Use int for counts and IDs, float for measurements and ratios, and decimal.Decimal when exact decimal money arithmetic is required. Never compare floats with ==; use round() or math.isclose() instead.
How it works
- Python evaluates price * quantity
- Are both operands int? Yes: Result is an int (except with /, which gives a float). No: Promote to float and compute.
- Is the operator /? Yes: Return a float even if it divides evenly. No: Apply the operator normally.
- Store the result as a new number object
- Output: 59.97
Syntax
x = 10 # int
y = 10.5 # floatExample
price = 19.99
quantity = 3
total = price * quantityKey points
- Division with / always returns a float
- Use // for integer (floor) division; it rounds down, so -7 // 2 is -4
- int has unlimited precision, so it never overflows
- Floats are approximations: 0.1 + 0.2 is not exactly 0.3
- Compare floats with math.isclose(), not ==
- Write 1_000_000 for readable big numbers