• Home
  • 3.11 Floor Division

3.11 Floor Division

View Categories

3.11 Floor Division

2 min read

Division does not always produce a whole number. In many situations, only the integer part of the quotient is required.

Python provides the floor division operator (//) for this purpose.

Floor division divides two numbers and returns the largest integer less than or equal to the actual quotient. In other words, it rounds the result down toward negative infinity.

Floor division is commonly used when calculating groups, pages, batches, rows, or any situation where only complete units are needed.


Syntax #

result = dividend // divisor

Components #

Component Description
dividend The number to be divided.
// Floor division operator.
divisor The number that divides the dividend.
result The quotient rounded down to the nearest whole number.

Example 1: Floor Division of Two Integers #

main.py

a = 17
b = 5

print(a // b)

Output #

3

Explanation #

  • 17 รท 5 equals 3.4.
  • Floor division removes the fractional part by rounding down.
  • Therefore, the result is 3.

Example 2: Division vs Floor Division #

main.py

a = 17
b = 5

print(a / b)
print(a // b)

Output #

3.4
3

Explanation #

  • / performs normal division and returns 3.4.
  • // performs floor division and returns 3.
  • Use the operator that matches the required result.

Example 3: Floor Division with Floating-Point Numbers #

main.py

print(17.5 // 4)

Output #

4.0

Explanation #

  • 17.5 รท 4 equals 4.375.
  • Floor division rounds the result down to 4.0.
  • Since one operand is a float, the result is also a float.

Example 4: Floor Division with Negative Numbers #

main.py

print(-17 // 5)

Output #

-4

Explanation #

  • -17 รท 5 equals -3.4.
  • Floor division rounds toward negative infinity.
  • The largest integer less than or equal to -3.4 is -4.

Example 5: Both Operands are Negative #

main.py

print(-20 // -3)

Output #

6

Explanation #

  • -20 รท -3 equals approximately 6.67.
  • Floor division rounds down to 6.
  • Since both numbers are negative, the result is positive.

Example 6: User Input #

main.py

total = int(input("Enter total items: "))
group = int(input("Enter group size: "))

print(total // group)

Sample Input #

23
5

Output #

4

Explanation #

  • 23 รท 5 equals 4.6.
  • Only four complete groups can be formed.
  • Floor division returns 4.

Example 7: Calculating Complete Boxes #

main.py

apples = 56
box_capacity = 8

boxes = apples // box_capacity

print(boxes)

Output #

7

Explanation #

  • Each box holds 8 apples.
  • 56 // 8 equals 7.
  • Seven complete boxes can be filled.

Example 8: Mixed Data Types #

main.py

print(25 // 4.0)

Output #

6.0

Explanation #

  • One operand is a floating-point number.
  • Therefore, Python returns a floating-point result.
  • The mathematical value is still rounded down.

Example 9: Exact Division #

main.py

print(20 // 4)

Output #

5

Explanation #

  • 20 is exactly divisible by 4.
  • No rounding is required.
  • The result is 5.

Example 10: Checking the Return Type #

main.py

print(type(20 // 4))
print(type(20.0 // 4))

Output #

<class 'int'>
<class 'float'>

Explanation #

  • When both operands are integers, the result is an integer.
  • If either operand is a floating-point number, the result is a float.

Common Mistakes #

1. Confusing / with // #

Incorrect

print(15 // 4)

Incorrect Expectation #

3.75

Actual Output #

3

Reason

// performs floor division, not normal division.

Correct

print(15 / 4)

2. Expecting Negative Results to Round Toward Zero #

Incorrect

print(-15 // 4)

Incorrect Expectation #

-3

Actual Output #

-4

Reason

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


3. Dividing by Zero #

Incorrect

print(10 // 0)

Output #

ZeroDivisionError: integer division or modulo by zero

Reason

Division by zero is not allowed.


4. Assuming the Original Variable Changes #

Incorrect

number = 19

number // 4

print(number)

Output #

19

Reason

The result of floor division is returned as a new value.
The original variable remains unchanged.

Correct

number = number // 4

print(number)

Best Practices #

  • Use // when only complete units are needed.
  • Remember that floor division rounds toward negative infinity.
  • Use / when the fractional part of the result is important.
  • Check for zero before performing floor division.
  • Store the result if it will be used later in the program.

Key Points to Remember #

  • // is the floor division operator in Python.
  • Floor division returns the quotient rounded down.
  • It rounds toward negative infinity, not toward zero.
  • When both operands are integers, the result is an integer.
  • If either operand is a floating-point number, the result is a float.
  • Floor division is useful for calculating complete groups, batches, pages, and containers.
  • Dividing by zero raises a ZeroDivisionError.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *