Skip to content

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.

Beginner 10 min read·Lesson 49 of 306 in Python Programming

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

Python
groups = total // size
whole, remainder = divmod(total, size)
  • // gives the number of complete groups.
  • divmod() returns the quotient and remainder together.
  • Both raise ZeroDivisionError if size is 0.

Code examples

Whole groups

Python
print(17 // 5)
print(20 // 4)
print(7.5 // 2)
Output
3
5
3.0

Negative numbers round down

Python
print(-7 // 2)
print(int(-7 / 2))
Output
-4
-3

// rounds toward minus infinity; int() truncates toward zero.

More examples

Pages, boxes and hours

Python
items = 47
per_page = 10
full_pages = items // per_page
pages_needed = (items + per_page - 1) // per_page
print("Full pages:", full_pages, "Pages needed:", pages_needed)
bottles, per_box = 130, 24
boxes, loose = divmod(bottles, per_box)
print(boxes, "boxes and", loose, "loose bottles")
minutes = 135
print(minutes // 60, "h", minutes % 60, "min")
Output
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:

TextOutput
Full pages: 4 Pages needed: 5
5 boxes and 10 loose bottles
2 h 15 min

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

-7 // 2 # expecting -3

Prefer

int(-7 / 2) # -3, if truncation is what you want

// floors, so negative results round away from zero.

Using // when you need to round up

Avoid

pages = items // per_page

Prefer

pages = (items + per_page - 1) // per_page

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

  1. 1
    How many full dozens are in 150 eggs, and how many eggs are left over?
  2. 2
    Convert 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?
Practise this topic