Post

Number operations in Python

The arithmetic operators in Python: + - * / // % and **, the difference between true and floor division, round, the math module, and why floats aren't exact.

Python does arithmetic the way you’d expect, with a few details that matter: / and // are different operations, and round() doesn’t always round halves up.

Every example here was run with Python 3.13. The rules were checked against the official numeric types docs.

The basic operators

print(7 + 2, 7 - 2, 7 * 2)
print(2 ** 10)
9 5 14
1024

** is the power operator. 2 ** 10 is 2 to the power of 10.

Operators follow the usual precedence: ** first, then *, /, // and %, then + and -. Use parentheses when you’re not sure:

print(2 + 3 * 4 ** 2)
50

4 ** 2 is 16, 3 * 16 is 48, and 2 + 48 is 50.

True division and floor division

/ always gives a float, and // rounds down to a whole number:

print(7 / 2)
print(7 // 2)
print(10 / 4 * 2)
3.5
3
5.0

// rounds toward minus infinity, not toward zero. The docs give the examples 1//2 is 0, (-1)//2 is -1, and 1//(-2) is -1. The same rule applies to floats:

print(1 // 0.5, -7.5 // 2)
2.0 -4.0

The remainder: %

% gives the remainder after division: 7 % 2 is 1. It has its own post: what it’s useful for, how it treats negative numbers, and divmod().

Assignment operators

+= from the variables post works with every operator here: -=, *=, /=, //=, %= and **=:

x = 5
x += 2
x *= 3
print(x)
21

Built-in functions

abs returns the absolute value. round rounds to the nearest number:

print(abs(-3.5))
print(round(2.5), round(3.5))
print(round(2.675, 2))
3.5
2 4
2.67

Python rounds halves to the nearest even number. round(2.675, 2) gives 2.67, because 2.675 is stored as a float slightly below 2.675.

The math module

For square roots, rounding up or down, and other functions, import math:

import math

print(math.sqrt(16), math.floor(3.7), math.ceil(3.2))
4.0 3 4

math.floor rounds down, and math.ceil rounds up. Both return an int.

Floats are not exact

Computers store most decimal fractions as approximations:

print(0.1 + 0.2)
0.30000000000000004

This isn’t a Python bug. It’s how binary floats work. For money or exact comparisons, use the decimal module, or compare with math.isclose() instead of ==.

What’s next

Next: type conversion, turning text into numbers and back.