0
0
PythonConceptBeginner · 3 min read

Floor Division in Python: What It Is and How to Use It

In Python, floor division is an operation that divides two numbers and rounds the result down to the nearest whole number. It uses the // operator and always returns an integer if both inputs are integers, or a float if any input is a float.
⚙️

How It Works

Floor division in Python divides one number by another and then rounds the result down to the nearest whole number. Imagine you have a pizza cut into slices, and you want to know how many whole slices each person gets without sharing any leftover pieces. Floor division tells you exactly that number.

For example, if you divide 7 by 3, the exact answer is about 2.33. Floor division rounds this down to 2, ignoring the decimal part. This is different from regular division, which keeps the decimal part.

💻

Example

This example shows how floor division works with integers and floats.

python
a = 7
b = 3
result_int = a // b

x = 7.0
y = 3
result_float = x // y

print("7 // 3 =", result_int)
print("7.0 // 3 =", result_float)
Output
7 // 3 = 2 7.0 // 3 = 2.0
🎯

When to Use

Use floor division when you need to divide numbers and want to ignore any fractional part, keeping only whole units. This is useful in situations like:

  • Splitting items evenly without leftovers, such as dividing candies among children.
  • Calculating how many full groups can be formed from a total count.
  • Working with indexes or positions where only whole numbers make sense.

It helps avoid errors when decimals are not meaningful or allowed.

Key Points

  • Floor division uses the // operator in Python.
  • It divides and rounds down to the nearest whole number.
  • Returns an integer if both inputs are integers, otherwise a float.
  • Different from regular division / which keeps decimals.
  • Useful for counting whole units or groups.

Key Takeaways

Floor division divides and rounds down to the nearest whole number using //.
It returns an integer if both inputs are integers, else a float.
Use it when you need whole units without fractions.
It differs from regular division which keeps decimal parts.