Python has two everyday number types: integers (int) for whole numbers and floats (float) for decimals.

Integers

An int is any whole number — positive, negative, or zero. There’s no upper limit; Python integers can grow as large as your computer’s memory allows.

count: int = 42
year: int = 2026
negative: int = -7
zero: int = 0
big_number: int = 10_000_000

You can use underscores to make long numbers easier to read. 10_000_000 and 10000000 mean the same thing.

Floats

A float is a number with a decimal point.

price: float = 9.99
gravity: float = 9.81
temperature: float = -3.5
ratio: float = 0.5

Floats can also be written using scientific notation:

small: float = 1.5e-3    # 0.0015
large: float = 2.0e6     # 2,000,000.0

The e notation means “times 10 to the power of the number that follows” — 1.5e-3 means 1.5 × 10⁻³, and 2.0e6 means 2.0 × 10⁶.

Basic arithmetic

Numbers support the usual operators:

print(10 + 3)    # 13   addition
print(10 - 3)    # 7    subtraction
print(10 * 3)    # 30   multiplication
print(10 / 3)    # 3.333...   true division (always returns float)
print(10 // 3)   # 3    integer division (drops the remainder)
print(10 % 3)    # 1    modulo (remainder)
print(10 ** 3)   # 1000 exponent (10 to the power 3)

Two operators surprise newcomers:

  • / always returns a float, even when both sides are integers. 10 / 2 is 5.0, not 5.
  • // drops the remainder. 7 // 2 is 3, not 3.5.

Mixing int and float

When you mix an int and a float in an expression, the result is a float:

result = 10 + 0.5
print(type(result), result)
<class 'float'> 10.5

Python promotes the integer to a float so the arithmetic works. You’ll see this pattern often when working with data.

The float trap

Floats can’t represent every decimal exactly. The classic surprise:

print(0.1 + 0.2)
0.30000000000000004

This isn’t a Python bug — it’s how floating-point numbers work in every language (Java, JavaScript, C++ all do the same thing). Floats are stored in binary, and some decimal numbers don’t have an exact binary form.

For most ML and scientific work, the tiny inaccuracy doesn’t matter. But:

  • Never use == to compare floats. Use a tolerance: abs(a - b) < 1e-9.
  • For money, use the decimal module (we’ll meet it in Section 14).
# wrong
print(0.1 + 0.2 == 0.3)   # False!

# right
print(abs((0.1 + 0.2) - 0.3) < 1e-9)   # True

Useful built-in number functions

A few handy functions that come with Python:

print(abs(-7))         # 7        absolute value
print(round(3.14159, 2))  # 3.14   round to N decimals
print(min(4, 9, 2))    # 2        smallest
print(max(4, 9, 2))    # 9        largest
print(pow(2, 10))      # 1024     same as 2 ** 10

We’ll meet many more in Section 14 when we tour the math and statistics modules.

Type conversion between int and float

You can turn a number from one type into the other:

x: float = 3.7
y: int = int(x)        # 3 — drops the decimal, doesn't round

z: int = 5
w: float = float(z)    # 5.0

int() always truncates toward zero, so int(-3.7) is -3, not -4. Use round() if you want rounding.

What’s next

You know Python’s numeric types. Next, we’ll meet booleans — the type with only two values, but more power than its size suggests.

Toggle theme (T)