w3resource

Floor Division in Python (//)

What Is Floor Division in Python?

Floor Division in Python (//)

Division is one of the most common operations in programming. Python offers two types of division:

  • True division (/)
  • Floor division (//)

This article focuses on floor division, how it works, and where beginners often get confused.

What Is Floor Division?

Floor division divides two numbers and returns the largest integer less than or equal to the result.

In simple words:

It divides and then rounds the result down toward negative infinity.

Syntax :

result = a // b

Basic Examples :


print(10 // 3)     # Output: 3
print(20 // 5)     # Output: 4
print(7 // 2)      # Output: 3

Explanation :

  • 10 / 3 = 3.333… → floor value = 3
  • 7 / 2 = 3.5 → floor value = 3

Floor Division vs True Division :

Expression Result :

7 / 2	3.5
7 // 2	3
/ gives a float result
// gives a floored result

Floor Division with Negative Numbers :

This is where most learners get stuck.


print(-7 // 2)   # Output: -4

Why -4 and not -3?

Because:

  • -7 / 2 = -3.5
  • Floor means rounding down, not truncating
  • The next lower integer after -3.5 is -4

Compare with Truncation :


int(-3.5)   # -3
-7 // 2     # -4

Key Rule :

Floor division always moves toward negative infinity, not toward zero.

Floor Division with Floats :

Floor division works with floating-point numbers too.


print(5.5 // 2)      # 2.0
print(9.0 // 2.0)    # 4.0
print(-5.5 // 2)     # -3.0

Note:

  • The result is a float if any operand is a float.
  • The floor rule still applies.

Relationship Between // and % :

Floor division is closely related to the modulus operator (%).

Python follows this identity:

a = (a // b) * b + (a % b)

Example :


a = -7
b = 2
print(a // b)   # -4
print(a % b)    # 1

Check :

(-4 × 2) + 1 = -7

This explains why modulo results may seem unusual with negative numbers.

Common Use Cases

1. Splitting Items Evenly


students = 23
groups = 5
print(students // groups)   # 4 students per group

2. Extracting Digits


number = 1234
print(number // 10)   # 123

3. Pagination Logic


items = 57
items_per_page = 10
pages = (items + items_per_page - 1) // items_per_page

Common Mistakes to Avoid :

  • Assuming // simply removes decimals
  • Expecting truncation behavior for negative numbers
  • Forgetting that floats produce float results

Quick Summary :

  • // performs floor division
  • Result is rounded down, not truncated
  • Works with integers and floats
  • Behavior with negative numbers is intentional and consistent
  • Strongly linked with % operator

Practice Exercises: Floor Division in Python (//)

Section A: Predict the Output

Write the output of each expression without using Python.

15 // 4
20 // 3
8 // 2
1 // 3

Section B: Negative Numbers (Concept Check)

Predict the output carefully.

-10 // 3
10 // -3
-10 // -3
-1 // 2

Section C: Float Operands

Determine the output and its data type.

9.0 // 2
9 // 2.0
-7.5 // 2
5.99 // 1

Section D: Relationship with Modulus

Without calculating directly in Python, verify the identity:

a = (a // b) * b + (a % b)
a = 17 , b = 5
a = -17 , b = 5
a = 17 , b = -5

Write :

  • a // b
  • a % b
  • Final value using the identity

Section E: Code Understanding

Predict the output of the following code snippets.


x = 27
y = 4
print(x // y, x / y)

a = -9
b = 2
print(a // b, a % b)

x = 5.0
y = 2
print(type(x // y))

Section F: Real-World Logic

Write the Python expression using floor division only.

    A total of 73 items must be packed into boxes holding 6 items each.

    Find the number of completely filled boxes.

    A number n is a 4-digit integer.

    Write an expression to remove its last two digits using floor division.

Section G: True or False

State whether the statement is True or False.

    Floor division always returns an integer.

    a // b is the same as int(a / b) for all values.

    Floor division rounds toward zero.

    Floor division works with float operands.

Section H: Debug the Thinking

Each statement contains a logical mistake. Identify it.

    "Since -7 / 2 = -3.5, floor division gives -3."

    "Using // removes decimals just like typecasting to int."

    "If both operands are integers, the result of // is always positive."

Previous : Software Frameworks

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.