Python Glossary Русский

all Python 3Arithmetic & operatorsterm

% remainder

arithmetic

The remainder (modulo) operator. The sign of the result follows the sign of the divisor. Widely used to test parity and periodicity.

Syntax

a % b

The sign rule has a surprising consequence: -123 % 10 is 7, not 3, so the familiar "last digit via % 10" trick breaks on negative numbers — take abs(n) first. When you need both the quotient and the remainder, divmod(a, b) returns them in one step, and a zero divisor raises ZeroDivisionError just like ordinary division.

Examples

print(10 % 3)  # → 1
print(7 % 2)  # → 1 (нечётное число)
print(8 % 2)  # → 0 (чётное число)
print(123 % 10)  # → 3 (последняя цифра)
seconds = 3661; print(seconds % 60)  # → 1 (секунды)
print(15 % 5)  # → 0 (кратно)
try:
    print(5 % 0)
except ZeroDivisionError:
    print("Деление на ноль")  # → Деление на ноль

See also

Open in the full glossarydocs.python.orgReport an inaccuracy