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?
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.
Hello, Can someone please explain the me the difference between those two arithmetic signs because am new to programming python:)
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.
Both are valid mathematical functions with different results.
modulus
The modulus-function computes the remainder of a division, which is the "leftover" of an integral division.
floor
The floor-function provides the lower-bound of an integral division. The upper-bound is computed by the ceil function. (Basically speaking, the floor-function cuts off all decimals).
a=5
b=2
print(a%b) # Prints 1 as leftover
print(a//b) # Prints 2, since 5/2=2.5 and the decimal is cut off
print(a - (a//b)*b) # Prints 1, this is the modulo, calculated by the floor function
Assume a= 10, b = 6
a%b will give you the remainder, that is 4
a//b will give you the quotient, that is 1