Last modified: Apr 04, 2026 By Alexander Williams
Python // Operator: Floor Division Guide
Python has many operators. One important operator is //. It is called the floor division operator.
This article explains what it does. We will cover its syntax, behavior, and practical uses. You will see clear examples.
What is the // Operator?
The // operator performs floor division. It divides two numbers and returns the largest integer less than or equal to the result.
Think of it as regular division, but you always round down to the nearest whole number. It works with integers and floats.
This is different from the standard division operator /. The single slash gives a float result. The double slash gives an integer.
Basic Syntax and Examples
The syntax is simple: dividend // divisor. Let's look at some basic calculations.
# Basic integer floor division
result1 = 10 // 3
print(result1) # What will this output?
result2 = 15 // 4
print(result2)
3
3
10 divided by 3 is approximately 3.33. The floor of 3.33 is 3. So 10 // 3 returns 3.
15 divided by 4 is 3.75. The floor is 3. The operator discards the decimal part completely.
Floor Division with Negative Numbers
Behavior with negative numbers can be surprising. Remember, floor means "round down" on the number line.
For a negative result, rounding down means moving to a more negative integer.
# Floor division with negative numbers
print(-10 // 3) # Result is -4, not -3
print(10 // -3) # Result is -4
print(-10 // -3) # Result is 3
-4
-4
3
-10 / 3 equals about -3.33. The floor of -3.33 is -4. This is the next integer down the number line.
This is a key difference from simple truncation. The int() function would truncate towards zero, giving -3.
Using // with Floating-Point Numbers
The operator also works with floats. The result is still a float, but it is rounded down to the nearest whole float value.
# Floor division with floats
print(10.0 // 3.0) # Result is 3.0
print(11.5 // 2.5) # 11.5 / 2.5 = 4.6, floor is 4.0
print(-7.5 // 2.0) # -7.5 / 2.0 = -3.75, floor is -4.0
3.0
4.0
-4.0
The data type of the result follows standard Python rules. If either operand is a float, the result is a float.
Practical Use Cases for Floor Division
Why is this operator useful? It has several practical applications in real programming.
1. Splitting Items into Whole Groups
Imagine you have 17 apples and baskets that hold 5 apples each. How many full baskets can you fill?
apples = 17
basket_capacity = 5
full_baskets = apples // basket_capacity
print(f"You can fill {full_baskets} full baskets.")
You can fill 3 full baskets.
The regular division / would give 3.4 baskets. Floor division gives the integer count of complete groups.
2. Converting Units
It is perfect for unit conversions where you need whole units. For example, converting total minutes into whole hours.
total_minutes = 145
hours = total_minutes // 60 # Get the whole hours
remaining_minutes = total_minutes % 60 # Get the leftover minutes
print(f"{total_minutes} minutes is {hours}h {remaining_minutes}min.")
145 minutes is 2h 25min.
Here, // gets the hours, and the modulo operator% gets the remainder. They are often used together.
3. Pagination in Web Development
Calculating page numbers for displaying data is a common task. If you have 48 items and show 10 per page, how many pages are needed?
total_items = 48
items_per_page = 10
# Calculate total pages, adding 1 for any partial last page
total_pages = (total_items + items_per_page - 1) // items_per_page
print(f"Total pages needed: {total_pages}")
Total pages needed: 5
The formula (n + d - 1) // d is a standard trick for ceiling division using floor.
// vs / vs %: Understanding the Differences
It is important to distinguish between three related operators.
/(Division): Returns a precise quotient, often a float.//(Floor Division): Returns the quotient rounded down to an integer (or float).%(Modulo): Returns the remainder of the division.
They form a mathematical relationship: dividend = (dividend // divisor) * divisor + (dividend % divisor).
dividend = 17
divisor = 5
quotient = dividend // divisor
remainder = dividend % divisor
print(f"Quotient: {quotient}, Remainder: {remainder}")
print(f"Check: ({quotient} * {divisor}) + {remainder} = {(quotient * divisor) + remainder}")
Quotient: 3, Remainder: 2
Check: (3 * 5) + 2 = 17
Common Pitfalls and Tips
Be aware of these common issues when using floor division.
Division by Zero: Just like normal division, dividing by zero with // causes a ZeroDivisionError.
Type of Result: The result is an integer only if both operands are integers. If a float is involved, the result is a float, even if it's a whole number like 3.0.
Not Truncation: For negative numbers, remember it floors (downwards), not truncates (towards zero). This is a major source of bugs.
Conclusion
The Python // floor division operator is a powerful tool. It provides a predictable way to get integer results from division.
You learned its syntax, how it works with positive, negative, and floating-point numbers. We explored practical uses like grouping items and pagination.
Remember its relationship with the modulo operator %. Understanding when to use // instead of / will make your code more robust and intentional.
Keep practicing with different numbers. This will solidify your understanding of this essential Python operator.