Module 4 · Operators — topic 6 of 16
Floor division
The // operator divides and rounds down to the nearest whole number. Learn its behaviour with negative numbers and floats, and practical uses for grouping, paging and unit conversion.
Learning objectives
- Use // to get whole-number quotients
- Predict // results for negative numbers
- Apply // to grouping and conversion problems
Definition
The // operator divides and rounds down to the nearest whole number. Learn its behaviour with negative numbers and floats, and practical uses for grouping, paging and unit conversion.
Introduction
Floor division with // divides and then rounds the result down to the nearest whole number. 17 // 5 is 3, because 5 fits into 17 three whole times.
Why it matters
Whenever you need “how many complete groups”, // is the tool: full boxes, complete pages, whole hours, rows in a grid. It is also one half of the quotient–remainder pair used with %.
Detailed explanation
For positive numbers, // simply drops the fractional part: 7 // 2 is 3.
“Floor” means rounding toward minus infinity, not toward zero. So -7 // 2 is -4, because -3.5 rounded down is -4. This differs from int(-3.5), which is -3.
With two ints, // returns an int. If either operand is a float, the result is a float with no fractional part: 7.0 // 2 is 3.0.
// and % are linked: for any b that isn’t zero, (a // b) * b + (a % b) == a. divmod(a, b) returns both at once.
Dividing by zero with // raises ZeroDivisionError, just like /.
When you need to round up rather than down, for example the number of buses required for a group, use math.ceil(a / b) or the integer trick (a + b - 1) // b. Both give the smallest whole number of groups that holds everything.
Syntax
- // gives the number of complete groups.
- divmod() returns the quotient and remainder together.
- Both raise ZeroDivisionError if size is 0.
Code examples
Whole groups
3 5 3.0
Negative numbers round down
-4 -3
// rounds toward minus infinity; int() truncates toward zero.
More examples
Pages, boxes and hours
Full pages: 4 Pages needed: 5 5 boxes and 10 loose bottles 2 h 15 min
Step-by-step explanation
- 47 // 10 is 4 full pages of ten items.
- (items + per_page - 1) // per_page rounds up instead of down, giving the 5 pages needed to show all items.
- divmod(130, 24) gives 5 full boxes and 10 loose bottles.
- 135 // 60 is 2 hours and 135 % 60 is 15 minutes.
Expected output
Running “Pages, boxes and hours” prints exactly:
Practical applications
- Web pages compute how many pages of search results to show with //.
- Warehouses calculate full cartons and leftovers with divmod().
- Time displays convert seconds to minutes and hours with // and %.
Best practices
- Use divmod() when you need quotient and remainder.
- Use the ceiling formula or math.ceil() to round up.
- Remember // floors negative numbers.
Common mistakes
Assuming // truncates negatives toward zero
Avoid
Prefer
// floors, so negative results round away from zero.
Using // when you need to round up
Avoid
Prefer
Plain // ignores a partially filled last page.
Key takeaways
- // divides and rounds down to a whole number.
- Negative results round toward minus infinity.
- (a // b) * b + a % b == a.
Try it yourself
- 1How many full dozens are in 150 eggs, and how many eggs are left over?
- 2Convert 1,000,000 seconds into whole days using //.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?