Python’s numeric types โ int and float โ behave similarly to their JavaScript counterparts but with some important differences. Python integers have unlimited precision โ there is no maximum safe integer like JavaScript’s Number.MAX_SAFE_INTEGER. Python floats use 64-bit IEEE 754 double precision, the same as JavaScript, which means the same floating-point precision issues apply. Understanding Python’s arithmetic operators, integer division, the modulo operator, and when to use the decimal module for exact arithmetic is essential for building a FastAPI application that handles financial data, pagination calculations, and numeric validation correctly.
Python’s Numeric Types
# int โ whole numbers, unlimited precision
small = 42
large = 9_999_999_999_999_999 # underscores for readability
negative = -17
hex_lit = 0xFF # 255
bin_lit = 0b1010 # 10
oct_lit = 0o17 # 15
# float โ 64-bit double precision
pi = 3.14159265358979
sci_small = 1.5e-10 # 0.00000000015
sci_large = 2.998e8 # 299800000.0
# complex โ Python supports complex numbers natively
c = 3 + 4j # real=3, imaginary=4
print(c.real, c.imag) # 3.0 4.0
# Check if an int
isinstance(42, int) # True
isinstance(3.14, int) # False
isinstance(3.14, float) # True
int type has no size limit. Unlike JavaScript where numbers above 2^53 lose precision, Python integers grow as large as memory allows. This is critical for cryptographic applications (which FastAPI’s JWT implementation uses) and for handling large database IDs. For numbers beyond 64-bit float precision in financial calculations, use the decimal module.1_000_000 instead of 1000000, 3.141_592_653 instead of 3.141592653. This is valid Python syntax (Python 3.6+) and makes large numbers much easier to read at a glance โ especially useful when defining database limits, timeout values, or file size constants in your FastAPI configuration.0.1 + 0.2 == 0.3 evaluates to False in Python. For financial calculations (prices, totals, tax) always use Python’s decimal.Decimal type, which provides exact decimal arithmetic. FastAPI applications handling payments must never use plain floats for monetary values.Arithmetic Operators
a, b = 17, 5
# Standard arithmetic
a + b # 22 โ addition
a - b # 12 โ subtraction
a * b # 85 โ multiplication
a ** b # 1419857 โ exponentiation (no Math.pow needed)
# Division โ TWO types in Python
a / b # 3.4 โ true division (always returns float)
a // b # 3 โ floor division (integer result, rounds toward -infinity)
# Modulo
a % b # 2 โ remainder after floor division
# Augmented assignment (same as JS)
x = 10
x += 5 # x = 15
x -= 3 # x = 12
x *= 2 # x = 24
x //= 5 # x = 4 (floor division assignment)
x **= 3 # x = 64 (exponentiation assignment)
# Note: Python has no ++ or -- operators
# Use x += 1 instead of x++
Floor Division vs True Division
# True division (/) โ always returns float
10 / 2 # 5.0 (not 5!)
7 / 2 # 3.5
-7 / 2 # -3.5
# Floor division (//) โ returns int (rounds toward -infinity)
10 // 2 # 5
7 // 2 # 3 (truncates toward -infinity)
-7 // 2 # -4 (rounds DOWN, not toward zero โ different from JS Math.trunc!)
# Modulo (%) โ remainder
17 % 5 # 2 (17 = 3ร5 + 2)
-17 % 5 # 3 (Python modulo always has same sign as divisor)
# Practical uses
# Pagination: how many full pages of 10 items?
total_items = 47
items_per_page = 10
full_pages = total_items // items_per_page # 4
remainder = total_items % items_per_page # 7
# Is a number even?
def is_even(n):
return n % 2 == 0
# Is a number divisible by another?
def divisible_by(n, d):
return n % d == 0
The math Module
import math
math.sqrt(16) # 4.0 โ square root
math.ceil(3.2) # 4 โ ceiling (round up)
math.floor(3.9) # 3 โ floor (round down)
math.abs(-5) # Note: use built-in abs(-5) = 5 instead
math.pow(2, 10) # 1024.0 โ exponentiation (returns float)
math.log(100, 10) # 2.0 โ log base 10
math.log(math.e) # 1.0 โ natural log
math.pi # 3.141592653589793
math.e # 2.718281828459045
math.inf # infinity
math.isnan(float('nan')) # True โ check for NaN
math.isinf(math.inf) # True โ check for infinity
# Rounding
round(3.5) # 4 (banker's rounding: rounds to even)
round(2.5) # 2 (rounds to even โ 2 is even)
round(3.456, 2) # 3.46 โ round to 2 decimal places
Floating-Point Precision and the Decimal Module
# The floating-point problem (same in Python and JavaScript)
0.1 + 0.2 # 0.30000000000000004 โ not 0.3!
0.1 + 0.2 == 0.3 # False โ bug waiting to happen in financial code
# Workaround 1: round for display
round(0.1 + 0.2, 10) == round(0.3, 10) # True
# Workaround 2: math.isclose() for comparisons
import math
math.isclose(0.1 + 0.2, 0.3) # True โ uses relative tolerance
# Workaround 3 (best for money): use Decimal
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("19.99") # Always pass as string!
tax_rate = Decimal("0.10")
tax = price * tax_rate # Decimal("1.999")
# Round to 2 decimal places
total = (price + tax).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(total) # 21.99
# Never do this โ loses precision:
wrong = Decimal(0.1) # Decimal('0.1000000000000000055511151231257827021181583404541015625')
Common Mistakes
Mistake 1 โ Expecting / to return an integer
โ Wrong โ assuming division returns an int like Python 2:
result = 10 / 2
print(type(result)) # <class 'float'> โ it's 5.0, not 5!
โ Correct โ use // for integer division:
result = 10 // 2 # 5 (int) โ
Mistake 2 โ Using float for monetary values
โ Wrong โ float arithmetic for prices:
price = 19.99
tax = price * 0.1
total = price + tax # 21.988999999999997 โ billing error!
โ Correct โ use Decimal for money:
from decimal import Decimal
price = Decimal("19.99")
tax = price * Decimal("0.1") # Decimal("1.999")
total = price + tax # Decimal("21.989") โ exact โ
Mistake 3 โ Using ++ for incrementing
โ Wrong โ JavaScript increment operator:
count = 0
count++ # SyntaxError in Python
โ Correct โ augmented assignment:
count = 0
count += 1 # โ
Quick Reference
| Operation | Code | Result |
|---|---|---|
| True division | 7 / 2 |
3.5 (float) |
| Floor division | 7 // 2 |
3 (int) |
| Modulo (remainder) | 17 % 5 |
2 |
| Exponentiation | 2 ** 10 |
1024 |
| Absolute value | abs(-5) |
5 |
| Round | round(3.456, 2) |
3.46 |
| Square root | math.sqrt(16) |
4.0 |
| Exact decimal | Decimal("19.99") |
Exact value |