Lessons · Python · // whole-number division
Whole-number division
a // b divides and keeps the whole number, dropping the fraction: 7 // 2 is 3.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
How many full boxes of twelve, how many whole minutes in 130 seconds, which page a row lands on: you want a count, not 10.8 of something.
How to think about it
Pair it with %: n // k tells you how many whole groups, n % k what is left. If you find yourself calling int() on a plain division, // was the operator you wanted.
Worked example
print(7 // 2)3.
print(7 / 2)3.5: plain division always gives a float.
print(130 // 60, 130 % 60)2 10: two whole minutes and ten seconds.
print(-7 // 2)-4: floor division rounds down, towards negative infinity, not towards zero.
Your turn
How many full dozens in n?
dozens = n 12
Solve one with the tests running
The trap
-7 // 2 is -4, not -3. Floor means down, and for negative numbers down is away from zero.
Practise // whole-number division on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.