Floor division in Python — the 5 // 2 example
Contents
What floor division is
Floor division gives the whole-number result from a division. It drops any decimal part. You use it when you only care about the integer portion.
How Python does it
In Python you use the // operator. It divides two numbers, then rounds down to the nearest integer. So this means 5 // 2 gives a whole number.
The 5 // 2 example
5 divided by 2 is 2.5. With floor division you get 2. So 5 // 2 equals 2. It’s simple and clean.
A note about negatives
Floor division can surprise you with negative numbers. For example, -5 // 2 gives -3. That’s because it rounds down toward negative infinity, not toward zero. Keep that in mind when you work with negative values.
Quick tips
If you only want the integer part of a positive division, use //. It’s faster and clearer than other tricks. But if you need rounding toward zero, use int() or math.trunc().
Wrap-up
Floor division is handy. It gives whole numbers fast. Try 5 // 2 in a Python shell to see it live. You’ll get 2 and you’ll remember how it works next time.


