Python’s Number Types Explained. Covering all numeric types

Python sorts numbers into different data types so it can efficiently manage memory, precision, and operations behind the scenes.

Let’s explore Python’s numeric data types in detail.

Integers

When you write x = 10, Python stores that as an integer (int).

Integers can be negative, zero, or positive, and unlike many languages, Python’s int has arbitrary precision. That means you can work with extremely big numbers like 99999999999999999999999 without difficulty.

We use integers to count, loop, or index things.

We use integers for counting, indexing, and just about anything that doesn’t need a decimal point.
(click on the image to open in a new tab)

Floats

Now, let’s say you write price = 19.99. Python interprets that as a floating-point number (float). These represent real numbers and they’re great for representing measurements, currency, or ratios.

Price calculation with floats.
(click on the image to open in a new tab)

The problem with floats is that they aren’t always exact. When you try printing 0.1 + 0.2 , you get 0.30000000000000004!

The floating-point surprise.
(click on the image to open in a new tab)

This is not a bug but rather how binary math works.

Floating-point numbers are useful, but remember that they’re not perfectly precise due to how computers store them.

Decimal and Fraction

When accuracy really matters (like in financial apps) floats can’t help us.

Python gives us two more options:

  • decimal.Decimal: High precision numeric type. Great for financial calculations.
  • fractions.Fraction: Represents numbers as ratios. Useful for math that needs exactness, like probability.

Decimal and Fraction are part of Python’s standard library. That means they come bundled with Python, and we don’t need to install anything extra.

You just have to import them first:

We must import Decimal and Fraction first.
(click on the image to open in a new tab)

Although not used daily, these items are invaluable in certain applications.

Use Decimal when working with currency or need accuracy. Use Fraction when you care about precision in ratios or equations.

Decimal is accurate for working with currency. Fraction is ideal for precision in math.
(click on the image to open in a new tab)

Complex Numbers

Complex numbers are not everyday stuff, but it’s critical for electrical engineering, physics, and some AI applications.

Complex numbers are critical for electrical engineering, physics, and some AI applications.
(click on the image to open in a new tab)

If you’re banging your head against the wall trying to grasp what is going on here, it’s all right. You might not use complex numbers unless you’re doing advanced math or engineering work. But Python supports them natively, so here they are.

Although my blog doesn’t support comments, feel free to reply via email or X.