Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What Does the // Operator Do?
In Python, the // operator is the floor division operator that divides two numbers and rounds the result down to the nearest integer. This is different from regular division (/) which returns a float result.
Syntax
The basic syntax for floor division is ?
a // b
Where a is the dividend and b is the divisor.
Basic Floor Division Example
Let's see how floor division works with positive numbers ?
a = 37
b = 11
print("First Number:", a)
print("Second Number:", b)
# Floor division
result = a // b
print("Floor Division Result:", result)
# Regular division for comparison
regular_result = a / b
print("Regular Division Result:", regular_result)
First Number: 37 Second Number: 11 Floor Division Result: 3 Regular Division Result: 3.3636363636363638
Floor Division with Negative Numbers
Floor division always rounds down to the nearest integer, which means it rounds toward negative infinity ?
# Negative dividend
a = -37
b = 11
print("First Number:", a)
print("Second Number:", b)
result = a // b
print("Floor Division Result:", result)
regular_result = a / b
print("Regular Division Result:", regular_result)
First Number: -37 Second Number: 11 Floor Division Result: -4 Regular Division Result: -3.3636363636363638
Notice that -37 // 11 gives -4 (not -3) because floor division rounds down toward negative infinity.
More Examples
Here are additional examples showing different scenarios ?
# Various floor division examples
examples = [
(15, 4),
(-15, 4),
(15, -4),
(-15, -4)
]
for a, b in examples:
floor_result = a // b
regular_result = a / b
print(f"{a} // {b} = {floor_result} (regular: {regular_result:.2f})")
15 // 4 = 3 (regular: 3.75) -15 // 4 = -4 (regular: -3.75) 15 // -4 = -4 (regular: -3.75) -15 // -4 = 3 (regular: 3.75)
Comparison
| Operator | Name | Result Type | Behavior |
|---|---|---|---|
/ |
Division | Float | Returns exact quotient |
// |
Floor Division | Integer | Rounds down to nearest integer |
Conclusion
The // operator performs floor division, always rounding down toward negative infinity. Use it when you need integer division results, especially in algorithms requiring whole number quotients.
