Technical skill practice
What Does // Do in Python? Floor Division with Negative Values
The // operator in Python performs floor division: its quotient is rounded downward rather than toward zero. Negative inputs expose the difference. It is not a comment marker. Integer operands produce an integer result here; introducing a float produces a float result even when the quotient has no fractional part.
Start with a small, explicit task
Assign synthetic signed positions to bins of width four. Positions immediately below zero should belong to the bin below the zero bin. Predict the results for -1 and -5 before running them; these are the cases that differ from truncating a division result toward zero.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_floor_division.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
[-2, -1, 0, 0, 1, 2]
2.0 float
zero divisor rejected
offsets = [-5, -1, 0, 3, 4, 8]
bins = [offset // 4 for offset in offsets]
assert bins == [-2, -1, 0, 0, 1, 2]
assert -1 // 4 == -1
assert int(-1 / 4) == 0
floated = 9.0 // 4
assert floated == 2.0 and isinstance(floated, float)
try:
4 // 0
except ZeroDivisionError:
zero_result = "zero divisor rejected"
else:
raise AssertionError("expected division failure")
print(bins)
print(floated, type(floated).__name__)
print(zero_result)
Explain the result
With a width of four, bin zero covers positions 0 through 3. Position -1 must therefore lie in bin -1, while -5 lies in bin -2. Rounding the mathematical quotients downward gives those results. int(-1 / 4) instead produces zero here, so it does not implement the same signed-bin rule.
The float check returns 2.0, which compares numerically equal to 2 but has a different type. The code checks both value and type rather than relying on printed appearance alone. Division by zero is explicitly rejected. These examples use small exact integers; they are not a claim that every float division avoids approximation.
Avoid these mistakes
Do not replace floor division with int(a / b) when negative values are possible. Keep the divisor nonzero and define whether negative widths are meaningful for your task. Python comments begin with #; using // as a comment marker creates an arithmetic expression or a syntax error.
Questions, answered
Does // always return an int?
No. The checked 9.0 // 4 expression produces the float 2.0.
Why is -1 // 4 equal to -1?
The quotient is floored downward, rather than truncated toward zero.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.