Python foundations

What Does // Mean in Python? Floor Division

See why 7 // 3 is 2 and -7 // 3 is -3. Compare // with /, truncation, and the remainder returned by divmod().

In Python, // means floor division. For integers, it returns the quotient rounded down. 7 // 3 is 2, and -7 // 3 is -3. The regular division operator / keeps the fractional part in a floating-point result.

assert 7 // 3 == 2
assert -7 // 3 == -3
assert 8 / 4 == 2.0
assert 8 // 4 == 2

For negative values, flooring and cutting off the fraction are different. int(-7 / 3) truncates toward zero and gives -2. Replacing // with int(a / b) changes the rule and can also lose precision for large integers.

Use the quotient and remainder together

For integer operands with a nonzero divisor, Python keeps the relationship a == b * (a // b) + (a % b). divmod(a, b) returns both values in one result. With a positive divisor of three, the remainder is zero, one, or two, even when the dividend is negative.

whole, remainder = divmod(125, 60)
assert (whole, remainder) == (2, 5)

whole, remainder = divmod(-7, 3)
assert (whole, remainder) == (-3, 2)
assert 3 * whole + remainder == -7

The first example splits 125 seconds into two minutes and five seconds. The second has a positive remainder of two even though it starts from a negative number. With integer operands, a nonzero remainder has the same sign as the divisor.

Use floor division for complete groups and buckets

Floor division answers how many complete groups fit into a nonnegative total. If a box holds 12 items, items // 12 counts full boxes. It does not tell you how many boxes to reserve for all items when a partial box is needed. For nonnegative integer items and positive capacity, (items + capacity - 1) // capacity gives the required number of boxes.

If you group signed coordinates into blocks, flooring can be exactly right. With blocks of width ten, position -1 belongs to the block just left of zero. -1 // 10 gives -1. Truncation would put that position in the same block as the coordinates starting at zero.

Check the operand types

If one operand is a built-in float, the result is a float. For example, 7.0 // 3 returns 2.0. Binary floating-point approximations can also shift a result near a boundary, so keep exact counts as integers. Other numeric types can define different rules for //.

Both / and // raise ZeroDivisionError when the divisor is zero for ordinary integer or float operands. Handle a zero denominator according to its meaning. Zero available groups could be invalid input, but the absence of observations might be a legitimate case in which there is simply no average to report.

Verify exact and partial groups

For a packing calculation, test zero items, one item, exactly one box, and one more than one box. If your domain includes negative positions, test values just below and above zero. Those cases show whether you wanted inclusive buckets, complete groups, or capacity for leftovers. The operator you choose should follow that interpretation.

Sources and further reading

Related reading

Browse Python foundations · Explore project tutorials

Search PythonProjects.org