Floor division in Python is performed using the // operator, which divides two numbers and returns the largest integer less than or equal to the result, effectively rounding down to the nearest whole number.
Syntax:
a // bBehavior:
For positive numbers, it behaves like truncating the decimal part (e.g.,
7 // 2 = 3).For negative numbers, it rounds toward negative infinity (e.g.,
-7 // 2 = -4, not-3).
Result type:
If both operands are integers, the result is an integer.
If at least one operand is a float, the result is a float (e.g.,
7.5 // 2 = 3.0).
Key differences from standard division (/):
/always returns a float (e.g.,7 / 2 = 3.5).//returns an integer or float depending on input types, but always rounds down.
Common use cases:
Calculating indices (e.g.,
mid = (left + right) // 2in binary search).Pagination:
total_pages = (total_items + items_per_page - 1) // items_per_page.Splitting data into chunks:
num_chunks = len(data) // chunk_size.
Alternative method: Use math.floor(x / y) to achieve the same result, though // is more efficient and idiomatic.
💡 Note: The
//operator is also known as integer division in Python, but it differs from other languages (like JavaScript) in how it handles negative numbers.
In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.
You can find a detailed description at PEP 238: Changing the Division Operator.
Answer from Eli Courtwright on Stack OverflowVideos
What Does Floor Mean in Python?
Why Would You Use Floor Division?
What are the Related Courses and Blogs Provided by The Knowledge Academy?
Hello, Can someone please explain the me the difference between those two arithmetic signs because am new to programming python:)
In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.
You can find a detailed description at PEP 238: Changing the Division Operator.
Python 2.x Clarification:
To clarify for the Python 2.x line, / is neither floor division nor true division.
/ is floor division when both args are int, but is true division when either of the args are float.
You can expand the operator to be more than a single character. For example you could do /int~ and /float~ to disambiguate which division operation to use. It's pretty verbose but if you can find a shorter term to signify which operation you can use that.
This can then be expanded to other operations to for example ensure that overflow behavior is specified for integer addition +sat32~.
Some options that don't involve namespaces:
- Python:
a // b - Visual Basic:
a \ b - Pascal:
a div b
Normal division is a / b in all the cases.
Use this. this will help you.
a,b = divmod(10,2)
it will return both value
There is a significant difference in performance if you use larger numbers.
Here is an example using both small and large numbers:
$ py27 -m timeit -s "a=123;b=7" "divmod(a,b)"
10000000 loops, best of 3: 0.0913 usec per loop
$ py27 -m timeit -s "a=123;b=7" "a//b;a%b"
10000000 loops, best of 3: 0.047 usec per loop
$ py27 -m timeit -s "a=123333333333333333333333333333333333333;b=7222222222222222222" "divmod(a,b)"
10000000 loops, best of 3: 0.165 usec per loop
$ py27 -m timeit -s "a=123333333333333333333333333333333333333;b=7222222222222222222" "a//b;a%b"
1000000 loops, best of 3: 0.232 usec per loop
Why the difference?
divmod() requires a function call while // and % are operators. There is additional overhead for a function call relative to operators. So when the cost of the computation is minimal, the overhead of calling a function is much greater than the actual cost of the calculation(s).
For larger numbers, divmod() is faster. divmod() calculates both the quotient and remainder at the same time and returns both of them. The // and % operators each calculate the quotient and remainder but only return one of the results.
divmod() has more overhead but only performs one division. // and % have less overhead but perform two divisions. As long as the overhead is large compared to time to perform division, divmod() will be slower. But once the cost of a division is greater than the overhead, divmod() will be faster.
The // operator explicitly floors the result. Quoting the Binary arithmetic operations documentation:
the result is that of mathematical division with the ‘floor’ function applied to the result.
Flooring is not the same thing as rounding to 0; flooring always moves to the lower integer value. See the math.floor() function:
Return the floor of x, the largest integer less than or equal to x.
For -6 // 4, first the result of -6 / 4 is calculated, so -1.5. Flooring then moves to the lower integer value, so -2.
If you want to round towards zero instead, you'll have to do so explicitly; you could do this with the int() function on true division:
>>> int(-6 / 4)
-1
int() removes the decimal portion, so always rounds towards zero instead.
Floor division will also round down to the next lowest number, not the next lowest absolute value.
6 // 4 = 1.5, which rounds down to 1, and up to 2.
-6 // 4 = -1.5, which rounds down to -2, and up to -1.