Numbers: int, float, Decimal & Fraction

Python offers four numeric types for different jobs: int for exact whole numbers of any size, float for fast approximate decimals, Decimal for exact base-10 math like money, and Fraction for exact rational numbers with no rounding at all.

Learn Numbers: int, float, Decimal & Fraction in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick…

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Choosing the right number type prevents a whole category of subtle bugs — especially the infamous "0.1 + 0.2" surprise that bites every programmer eventually.

Python ints never overflow — they grow as large as needed. Floats, however, are binary approximations, so some decimals can't be stored exactly. That's why 0.1 + 0.2 isn't quite 0.3 :

decimal.Decimal stores numbers in base 10, exactly as written, so currency arithmetic is precise. The golden rule: build Decimals from strings , never from floats (which would import the float's error):

Use quantize() to round to cents, and you have correct, auditable money math — exactly what banking and e-commerce code requires.

fractions.Fraction keeps exact ratios with no rounding ever. round() and f-string formatting control how numbers display. And complex handles imaginary numbers when you need them:

Replace each ___ so the cart total is exact to the cent. Remember: build Decimals from strings.

Split a restaurant bill three ways using Decimal so the cents always add up correctly — and give the leftover penny to the first person.

Lesson complete — you pick the right number type every time!

You understand that int is unlimited, why 0.1 + 0.2 drifts, how Decimal keeps money exact, how Fraction gives perfect ratios, and how to round and format numbers cleanly — never comparing floats with == again.

🚀 Up next: Strings, Bytes & Unicode — encode and decode text the right way.

Practice quiz

What does 0.1 + 0.2 == 0.3 evaluate to in Python?

  • True
  • An error
  • False
  • None

Answer: False. Float rounding makes 0.1 + 0.2 equal 0.30000000000000004, so the comparison is False.

What is the safe way to compare two floats for near-equality?

  • math.isclose(a, b)
  • ==
  • round(a) == round(b)
  • a is b

Answer: math.isclose(a, b). math.isclose(a, b) checks whether floats are effectively equal despite rounding.

How large can a Python int get?

  • 32 bits
  • 64 bits
  • Up to 2**53
  • As big as memory allows (arbitrary precision)

Answer: As big as memory allows (arbitrary precision). Python ints have arbitrary precision and grow as needed, so 2**100 is exact.

Which type should you use for exact money math?

  • float
  • decimal.Decimal
  • complex
  • int only

Answer: decimal.Decimal. Decimal stores numbers in base 10 exactly, avoiding float rounding errors with currency.

What does Decimal('0.1') + Decimal('0.2') equal?

  • 0.3
  • 0.30000000000000004
  • 0.30
  • an error

Answer: 0.3. Decimals built from strings are exact, so the sum is exactly 0.3.

Why should you avoid Decimal(0.1) (from a float)?

  • It is faster but wrong
  • It raises an error
  • It imports the float's rounding error
  • It rounds to 0

Answer: It imports the float's rounding error. Building a Decimal from a float drags in the float's tiny error; build from a string instead.

What does Fraction(1, 3) + Fraction(1, 6) equal?

  • Fraction(2, 9)
  • Fraction(1, 2)
  • 0.5
  • Fraction(1, 9)

Answer: Fraction(1, 2). 1/3 + 1/6 = 1/2 exactly, with no rounding.

What does round(3.14159, 2) return?

  • 3.1
  • 3.142
  • 3
  • 3.14

Answer: 3.14. round() to 2 decimal places gives 3.14.

For z = 2 + 3j, what are z.real and z.imag?

  • 3.0 and 2.0
  • 2.0 and 3.0
  • 2 and 3j
  • 5.0 and 0.0

Answer: 2.0 and 3.0. j is the imaginary unit, so z.real is 2.0 and z.imag is 3.0.

Which method rounds a Decimal to a fixed number of places like cents?

  • round_to()
  • fix(2)
  • quantize(Decimal('0.01'))
  • to_cents()

Answer: quantize(Decimal('0.01')). quantize(Decimal('0.01')) rounds a Decimal to two places for money.