Floor Division in Python


TL ; DR:

  • Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

    print(10 // 2) # Output: 5
    print(15 // 4) # Output: 3
    print(20 // 3) # Output: 6
    

  • It can also be used with variables:

    a = 3
    b = 7
    
    print(20 // a) # Output: 6
    print(b // a) # Output: 2
    





Full lesson:

Remember how we first learn about the division of two integer numbers in primary school?

The quotient is the number of times a division is completed fully, while the remainder is the amount left that doesn't entirely go into the divisor.

Here are some examples:

10 / 2 = quotient 5, remainder 0
15 / 4 = quotient 3, remainder 3
20 / 3 = quotient 6, remainder 2

Floor division

Floor division (//) is a normal division operation except that it returns the integral part of the result (the quotient):

print(10 // 2) # Output: 5
print(15 // 4) # Output: 3
print(20 // 3) # Output: 6

It can also be used with variables:

a = 3
b = 7

print(20 // a) # Output: 6
print(b // a) # Output: 2

Modulo

The modulo operator (%) calculates the remainder of dividing two values:

print(10 % 2) # Output: 0
print(15 % 4) # Output: 3
print(20 % 3) # Output: 2

# Can be used with variables:
a = 2
b = 4

print(b % a) # Output: 0
print(11 % b) # Output: 3

Quotient and remainder

In programming, we combine both these concepts to get the quotient and remainder of some divison:

# Let's divide 26 by 3:
quotient = 26 // 3
remainder = 26 % 3

print(quotient) # Output: 8
print(remainder) # Output: 2

Assignment
Follow the Coding Tutorial and let's practice with quotient and remainder!


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the concept of floor division in Python. Floor division is a mathematical operation that divides two numbers and returns the largest integer less than or equal to the result. This operation is particularly useful in scenarios where you need to divide quantities into whole parts, such as distributing items evenly among groups.

Understanding the Basics

Before diving into floor division, it's essential to understand the basic division operation. In Python, the division operator (/) returns the quotient of two numbers, including the decimal part. For example:

print(10 / 3) # Output: 3.3333333333333335

However, in many cases, we are only interested in the whole number part of the division. This is where floor division comes into play. The floor division operator (//) returns the largest integer less than or equal to the result of the division:

print(10 // 3) # Output: 3

Main Concepts

Let's delve deeper into the key concepts of floor division:

  • Quotient: The result of the division without the remainder.
  • Remainder: The part of the division that is left over after the quotient is obtained.

For example, when dividing 15 by 4:

quotient = 15 // 4 # Output: 3
remainder = 15 % 4 # Output: 3

Examples and Use Cases

Let's look at some examples to understand how floor division works in different contexts:

# Example 1: Dividing 20 by 3
print(20 // 3) # Output: 6

# Example 2: Using variables
a = 5
b = 14
print(b // a) # Output: 2

# Example 3: Combining floor division and modulo
num = 26
divisor = 4
quotient = num // divisor
remainder = num % divisor
print(f"Quotient: {quotient}, Remainder: {remainder}") # Output: Quotient: 6, Remainder: 2

Common Pitfalls and Best Practices

When using floor division, it's important to be aware of some common pitfalls:

  • Negative Numbers: Floor division with negative numbers can yield unexpected results. For example:
  • print(-10 // 3) # Output: -4
    
  • Data Types: Ensure that the operands are of compatible data types to avoid type errors.

Best practices for using floor division include:

  • Always validate input values to avoid division by zero errors.
  • Use meaningful variable names to improve code readability.

Advanced Techniques

In advanced scenarios, you might need to combine floor division with other operations. For example, you can use floor division to implement pagination in a web application:

# Calculate the number of pages needed to display items
items_per_page = 10
total_items = 95
total_pages = (total_items + items_per_page - 1) // items_per_page
print(total_pages) # Output: 10

Code Implementation

Here is a complete example demonstrating the use of floor division and modulo operations:

# Function to divide two numbers and return quotient and remainder
def divide_numbers(dividend, divisor):
    quotient = dividend // divisor
    remainder = dividend % divisor
    return quotient, remainder

# Test the function
dividend = 26
divisor = 3
quotient, remainder = divide_numbers(dividend, divisor)
print(f"Quotient: {quotient}, Remainder: {remainder}") # Output: Quotient: 8, Remainder: 2

Debugging and Testing

When debugging code involving floor division, consider the following tips:

  • Print intermediate results to verify the correctness of calculations.
  • Use assertions to check for expected outcomes.

Example of a test case:

def test_divide_numbers():
    assert divide_numbers(26, 3) == (8, 2)
    assert divide_numbers(15, 4) == (3, 3)
    assert divide_numbers(20, 5) == (4, 0)
    print("All tests passed.")

# Run the test
test_divide_numbers()

Thinking and Problem-Solving Tips

When approaching problems related to floor division, consider the following strategies:

  • Break down the problem into smaller parts and solve each part individually.
  • Use diagrams or visual aids to understand the division process better.
  • Practice with different examples to gain a deeper understanding of the concept.

Conclusion

In this lesson, we explored the concept of floor division in Python. We learned how to use the floor division operator to obtain the quotient of a division and the modulo operator to get the remainder. Understanding these concepts is crucial for solving problems that involve dividing quantities into whole parts. Keep practicing and applying these concepts to become proficient in using floor division in your programs.

Additional Resources

For further reading and practice, consider the following resources: