HOWTO · Python
How to Get Division Remainder in Python
This tutorial demonstrates how to get the remainder of the division in Python. Learn about the modulus operator, the divmod function, and alternative methods to effectively calculate division remainders. Whether you're a beginner or an experienced programmer, our clear examples and explanations will help you master this essential programming concept.
On this page
To get the remainder of a division in Python, use the modulo operator %. For example, 10 % 3 is 1, while 10 % 2 is 0. Use divmod() when you need the quotient and remainder together. The sections below explain how each approach works, including Python’s rules for negative operands and what happens when the divisor is zero.
Use the Modulo Operator (%) to Get the Remainder
The % operator returns the remainder associated with Python’s floor-division result. With positive integers, this matches the familiar arithmetic remainder. The left operand is the number being divided and the right operand is the divisor. Run this example:
a = 10
b = 3
remainder = a % b
print(remainder)
1
The same operation with 10 % 2 evaluates to 0, because 10 is evenly divisible by 2. Use % when the remainder is the value your code needs, such as checking whether an integer is divisible by another integer with number % divisor == 0.
The divisor cannot be zero: evaluating a remainder with a zero divisor raises ZeroDivisionError. If the divisor comes from user input or another changing value, validate it before calculating, or handle that exception at the point where the operation occurs. Integer operands are the clearest choice for divisibility checks. Python also accepts floating-point operands, but binary floating-point representation can make results surprising when the decimal inputs cannot be represented exactly; avoid treating such results as exact decimal arithmetic.
Get the Quotient and Remainder with divmod()
The built-in divmod(a, b) returns a pair (a // b, a % b): the floor-division quotient followed by the remainder. Unpack the pair when you need both values, or select the second item when you only need the remainder. For example, quotient, remainder = divmod(10, 3) binds the quotient to 3 and the remainder to 1. This keeps the relationship between the two results explicit and avoids writing two separate expressions.
divmod() follows the same sign and zero-divisor rules as // and %. It does not make a zero divisor valid; divmod(10, 0) raises ZeroDivisionError. Choose it when subsequent logic needs both parts of the division, such as splitting a total into groups and tracking what is left over. If only the remainder matters, % communicates that intent more directly.
Understand Negative Operands and Division by Zero
Python’s integer division rounds down toward negative infinity, and % returns a remainder whose sign follows the divisor (unless the remainder is zero). The values satisfy a == (a // b) * b + (a % b). For instance, -10 % 3 is 2: -10 // 3 is -4, and -10 == (-4 * 3) + 2. With a negative divisor, 10 % -3 is -2, paired with 10 // -3 == -4.
This behavior differs from languages or conventions that truncate the quotient toward zero and give the remainder the dividend’s sign. When translating an algorithm, check which convention it expects rather than assuming all languages calculate remainder identically. The identity above is a useful way to check an integer result: the quotient and remainder must reconstruct the original dividend, and a nonzero remainder has the divisor’s sign.
A zero divisor has no corresponding quotient or remainder, so Python raises ZeroDivisionError for %, //, and divmod(). Check that the divisor is nonzero before the operation when that is a normal input condition. If zero indicates an exceptional input, catch ZeroDivisionError where the application can respond usefully; do not substitute an arbitrary remainder, since that changes the calculation’s meaning.
Calculate the Remainder with Floor Division and Subtraction
The remainder is related to floor division by the identity remainder = dividend - (dividend // divisor) * divisor. For example, with positive integer values, 10 - (10 // 3) * 3 gives 1. This formula explains why the quotient’s rounding rule matters: Python uses floor division, so the formula agrees with % for negative values too. A custom function can wrap the relationship when a named operation helps organize a larger calculation: def remainder(a, b): return a - (a // b) * b.
The function still needs a nonzero divisor; otherwise its // operation raises ZeroDivisionError. For ordinary Python code, use % for a remainder and divmod() for the paired values. The formula is most useful for explaining the relationship or implementing a deliberately named helper. It is not a reason to assume that an independently written formula will be faster or clearer than the built-in operators.
Choose the Right Method
Use % when you need only the remainder, for example to test divisibility. Use divmod() when both the floor-division quotient and remainder are useful. Use the floor-division-and-subtraction formula to understand how those values fit together, or place it in a custom function when the surrounding code benefits from that reusable name. In every case, account for Python’s floor-based behavior with negative operands and ensure the divisor is not zero.