Python Essentials for AI Engineer

Course Content

Python Essentials for AI Engineer

6 sections · 48 lessons

What is the difference between int and float?


What really happens when you type 0.1You write 0.1Stored as thenearestbinary fractionReal value0.1000000000000000055…Errors add up:0.30000000000000004Compare with math.isclose; keep money as integer paise.
The error lives in the storage, not the arithmetic, so no rounding at the end makes == safe for floats.

What you need to know

int: exact and unbounded

Python's int has no fixed size. Most languages stop at 64 bits; Python just uses more memory.

Python
print(2 ** 100)        # 1267650600228229401496703205376print(7 / 2)           # 3.5   -> true division always gives a floatprint(7 // 2)          # 3     -> floor divisionprint(-7 // 2)         # -4    -> floors towards minus infinity, not towards 0print(int(-3.7))       # -3    -> int() truncates towards 0

float: fast, but binary

A float stores a number as a binary fraction in 64 bits. Just as 1/3 cannot be written exactly in decimal (0.3333...), 0.1 cannot be written exactly in binary. Python stores the nearest possible value and hides the tail when printing:

Python
from decimal import Decimalimport mathprint(Decimal(0.1))         # 0.1000000000000000055511151231257827021181583404541015625print(0.1 + 0.2)            # 0.30000000000000004print(0.1 + 0.2 == 0.3)     # Falseprint(math.isclose(0.1 + 0.2, 0.3))  # Trueprint(round(2.5), round(3.5))        # 2 4  -> ties round to the even number

Floats also have special values: float("inf") and float("nan") (not a number). nan is not equal to anything, even itself, so you test it with math.isnan(x).

In ML you meet smaller floats

NumPy defaults to 64-bit floats, but PyTorch defaults to 32-bit (float32), and models are often trained or served in 16-bit formats (float16, bfloat16) to save GPU memory. Fewer bits means less precision, which is why a model can produce slightly different numbers on different hardware.

A real-life example

A payments dashboard adds up UPI transactions in rupees as floats:

Python
from decimal import Decimaltotal = 0.0for amount in [0.1, 0.2, 0.3]:    total += amountprint(total, total == 0.6)          # 0.6000000000000001 Falsepaise = [10, 20, 30]                # store money as integer paiseprint(sum(paise) / 100)             # 0.6exact = sum([Decimal("0.1"), Decimal("0.2"), Decimal("0.3")])print(exact, exact == Decimal("0.6"))   # 0.6 True

Across millions of rows, those tiny errors add up, and a reconciliation check that uses == fails even though no money is missing. The fix used by most payment systems is to store integer paise (or cents) and only format as rupees for display. Build Decimal from strings, not floats — Decimal(0.1) copies the float's error.

Follow-up questions to expect

  • "Why is 0.1 + 0.2 not 0.3?" — Both numbers are stored as the nearest binary fraction, and the two small errors do not cancel, so the sum is the double just above 0.3.
  • "What is NaN and how do you check for it?" — "Not a Number", the result of undefined maths like inf - inf. Use math.isnan(x), because nan == nan is False.
  • "int(3.9) or round(3.9)?" — int truncates to 3; round gives 4. For negatives, int(-3.9) is -3 and math.floor(-3.9) is -4.