Division: the two ways
Division in Python has a trap that catches almost every beginner — and plenty of experienced developers too.
There are two division operators and they behave very differently.
Regular division /
The / operator always returns a float — even when the result is a whole number:
Notice that 10 / 2 gives 5.0, not 5. Python promotes the result to a float every single time.
Floor division //
The // operator divides and then rounds down to the nearest whole number:
7 // 2 gives 3 — not 3.5, not 4. It always rounds down, never up.
The difference matters
If you used / here you'd get 2.5 minutes — which isn't what you want. // and % work together naturally whenever you're splitting things into groups.
Negative numbers round down too
"Round down" means toward negative infinity — not toward zero:
This surprises people. -7 / 2 is -3.5, and rounding down from -3.5 gives -4. Worth knowing before it bites you.
Key takeaways
/always returns a float — even10 / 2gives5.0//returns a whole number, always rounding down//and%are a natural pair — use them together when splitting into groups- Floor division rounds toward negative infinity, not toward zero
No comments yet. Add the first comment to start the discussion.