Python Floor Division (//)

Mastering integer division for robust mathematical logic

What is Floor Division?

Definition: Floor division, denoted by the double slash operator //, divides two numbers and rounds the result down to the nearest integer. It is sometimes called "integer division".

Standard Division vs. Floor Division

In Python 3, the standard single slash / always performs true division and returns a float, even if the result is a whole number.

Examples

# Standard Division (Returns float) print(10 / 3) # Output: 3.3333333333333335 (float) print(10 / 2) # Output: 5.0 (float) # Floor Division (Returns int*) print(10 // 3) # Output: 3 (int) print(10 // 2) # Output: 5 (int)
*Note: If one of the operands is a float, the result will still be rounded down, but returned as a float (e.g., 10.0 // 3 returns 3.0).

How It Works with Negative Numbers

This is where "floor" becomes important. "Floor" means rounding towards negative infinity (to the left on the number line), not just chopping off the decimal.

print(10 // 3) # 3.33 -> rounds down to 3 print(-10 // 3) # -3.33 -> rounds down to -4! # Note: simply truncating would give -3, but floor gives -4.

Common Use Cases

1. Calculating Whole Units

When you need to know how many full items fit into a container.

total_seconds = 365 minutes = total_seconds // 60 # Result: 6 full minutes

2. Index Calculation

Useful for finding the middle index of a list or converting linear indices to grid coordinates.

def get_middle(lst): mid_index = len(lst) // 2 return lst[mid_index]

3. Digit Extraction

Floor division is often paired with the modulo operator (%) to parse numbers digit by digit.

num = 123 last_digit = num % 10 # 3 remaining = num // 10 # 12

Related Topics