Stop 02 · Variables, Data Types & OperatorsTopics · 2/8

Numbers: int, float & Math

Part of the Python Roadmap

In short

Prices, marks, distances, percentages: almost every program crunches numbers. Python makes whole numbers effortless, but decimals have one famous surprise you need to know before you handle money.

int holds whole numbers of any size and float holds decimals with limited precision, so 0.1 + 0.2 is not exactly 0.3. The math module and built-ins like round(), abs() and pow() cover everyday calculations.

The jaadu counts shiny coins into neat stacks on one side of a tailor's table while measuring a long roll of cloth with a tape on the other.

Think of it like this

Counting coins vs measuring cloth

Counting coins gives an exact answer: 7 coins is 7 coins, however many you have. Measuring cloth with a tape is always slightly approximate, 1.5 metres is really 1.4998 or 1.5003. Python's int is counting coins, exact and unlimited. float is the measuring tape, very close but not perfectly exact.

Watch it happen

int or float?

int

  • Whole numbers: 42, -7, 0, 1_000_000
  • Exact, and can grow as big as memory allows
  • Perfect for counts, ages, quantities, paise
  • int("25") converts text to an int

float

  • Decimals: 3.14, 99.5, 2.0, 1e6
  • About 15-17 significant digits of precision
  • 0.1 + 0.2 gives 0.30000000000000004
  • Good for measurements, averages, science

Use int whenever the value is a count. For money, either store paise as int or use the decimal module.

The big ideas

What you need to know

ints never overflow

2 ** 100 prints all 31 digits exactly. Python grows the number as needed, unlike many languages with fixed-size integers.

floats are approximate

Computers store floats in binary, and 0.1 has no exact binary form. Compare floats with math.isclose(), never with ==.

Handy built-ins

round(x, 2), abs(-5), pow(2, 8), min(), max() and sum() need no import. Note that round(2.5) gives 2, because Python rounds halves to the nearest even number.

The math module

import math gives math.sqrt, math.ceil, math.floor, math.pi and more. For exact money maths, use from decimal import Decimal.

See it in code

Walk through the code

python
1import math2from decimal import Decimal3 4print(2 ** 100)                       # exact, 31 digits5print(0.1 + 0.2)                      # 0.300000000000000046print(0.1 + 0.2 == 0.3)               # False7print(math.isclose(0.1 + 0.2, 0.3))   # True8 9print(round(2.675, 2))                # 2.67, not 2.6810print(abs(-450), pow(2, 10))          # 450 102411print(math.sqrt(144), math.ceil(4.1), math.floor(4.9))  # 12.0 5 412 13print(Decimal("0.10") + Decimal("0.20"))  # 0.30, exact

1/5A huge int, printed exactly. No overflow, no scientific notation.

The comments show what each line prints. The float surprises are normal, not a Python bug.

Save yourself the pain

Common mistakes

Oops

Checking if total == 0.3 after adding floats.

Do this instead

Use math.isclose(total, 0.3), or round both sides to the precision you care about.

Oops

Storing rupee amounts as floats in a billing app and seeing totals off by a paisa.

Do this instead

Store amounts in paise as int (₹12.50 becomes 1250), or use Decimal("12.50") built from a string.

Oops

Writing Decimal(0.1) and expecting an exact value.

Do this instead

Pass a string: Decimal("0.1"). Passing a float copies the float's tiny error into the Decimal.

Your mission

Number experiments

~20 min

Quick check

What does print(0.1 + 0.2 == 0.3) show?

Go deeper

  1. 1Try 0.1 + 0.2 in the REPL and then round the result to 2 places.
  2. 2Use math.sqrt, math.ceil and math.floor on a few values.
  3. 3Use the decimal module when you need exact rupee-and-paise money calculations.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge