Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Floor Division in Python

Last Updated on April 10, 2023 by Prepbytes

In Python, floor division is a mathematical operation that rounds down the result of a division operation to the nearest integer. The floor division operator is represented by two forward slashes (//) in Python. In this article, we will discuss floor division in Python, how it works, and provide some code examples.

Understanding Floor Division in Python

Floor division in Python is similar to integer division, except that it always rounds down the result to the nearest integer.

Example of Floor Division in Python

If we divide 5 by 2 using integer division, the result would be 2. However, if we use floor division to divide 5 by 2, the result would be 2 as well, since 2 is the nearest integer that is less than the exact result of the division operation.

Code:

# Integer Division
print("Integer Division:")
print(5/2)


# Floor Division
print()
print("Floor Division:")
print(5//2)

Output:

Integer Division:
2.5

Floor Division:
2

Explanation:
The above code shows the result of the integer division and floor division when 5 is divided by 2.

In Python 3.x, the result of the division operation is always a float, even if the operands are integers. Therefore, when we use floor division on two integers, we get an integer result rounded down to the nearest integer.

Floor Division with Negative Numbers

If we use floor division with negative numbers, the result is still rounded down to the nearest integer, but we need to be careful about the sign of the result.

Code:

# Integer Division
print("Integer Division:")
print(-5/2)


# Floor Division
print()
print("Floor Division:")
print(-5//2)

Output:

Integer Division:
-2.5

Floor Division:
-3

Explanation:
In the above code, we divide -5 by 2 using floor division, the result is be -3, since -3 is the nearest integer that is less than the exact result of the division operation.
This is because floor division always rounds down the result, regardless of whether the operands are positive or negative.

Floor Division with Variables

Floor division can be used with variables as well. For example, if we have two variables x and y, and we want to perform floor division on them, we can simply use the // operator as shown in the code given below.

Code:

x = 5
y = 2


print(x//y)

Output:

2

Explanation:
In this example, first, we declared two variables x and y, and then the result of the floor division operation is assigned to the variable result.

Floor Division with Mixed Types

When we perform floor division on operands of different types, Python automatically converts them to a common type before performing the division operation. For example, if we divide an integer by a float using floor division, the integer is automatically converted to a float before the division operation is performed.

Code:

# Floor Division
print("Floor Division:")
print(5//2.0)

Output:

Floor Division:
2.0

Explanation:
In this example, the integer 5 is automatically converted to a float before the floor division operation is performed, resulting in a float result of 2.0.

Floor Division with Zero

When we perform floor division with a divisor of zero, Python raises a ZeroDivisionError exception.

Code:

# Floor Division
print("Floor Division:")
print(5//0)

Output:

Floor Division:
Traceback (most recent call last):
  File "d:\a.py", line 3, in 
    print(5//0)
ZeroDivisionError: integer division or modulo by zero

Explanation:
In this example, we tried to divide 5 by 0 using floor division, resulting in a ZeroDivisionError.

Floor Division with Modulus Operator

Floor division can be used in conjunction with the modulus operator (%) to obtain both the quotient and remainder of a division operation. For example, if we divide 5 by 2 using floor division and modulus, we can obtain both the quotient (2) and the remainder (1) of the division operation:

Code:

# Calculating Quotient
print("Quotient: ")
print(5//2)


# Calculating Remainder
print("Remainder: ")
print(5%2)

Output:

Quotient:
2
Remainder:
1

Explanation:
In this example, we use floor division to obtain the quotient of the division operation, and the modulus operator to obtain the remainder.

Conclusion
To conclude, floor division is a useful tool in Python for performing division operations that require rounding down the result to the nearest integer. In this article, we covered the basics of floor division in Python, including how it works, how to use it with variables and mixed types, and how to combine it with the modulus operator to obtain both the quotient and remainder of a division operation. We also discussed some potential pitfalls to be aware of, such as performing floor division with a divisor of zero, and the difference between floor division and integer division.

Frequently Asked Questions (FAQs)

Some Frequently Asked Questions on Floor Division Operator in Python are given below.

Ques 1. How is floor division different from integer division?
Ans. Floor division always rounds down the result of a division operation, while integer division always rounds toward zero.

Ques 2. Can you combine floor division with the modulus operator?
Ans. Yes, you can combine floor division with the modulus operator to obtain both the quotient and remainder of a division operation.

Ques 3. Can you perform floor division on a string?
Ans. No, floor division cannot be performed on a string.

Ques 4. Can you use floor division with boolean values?
Ans. No, floor division cannot be used with boolean values.

Ques 5. Can you use floor division with NaN?
Ans. No, floor division cannot be used with NaN (Not a Number) values.

Ques 6. How does floor division differ from truncation?
Ans. Floor division always rounds down the result of a division operation, while truncation simply removes the decimal part of a number.

Leave a Reply

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