Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:
>>> 6/3 2 >>> 6//3 2
Modulo − Represents as % operator. And gives the value of the remainder of an integer division. Division − represents as / operator. And gives the value of the quotient of a division.
The slash is an oblique slanting line punctuation mark /. Once used to mark periods and commas, the slash is now used to represent exclusive or inclusive or, division and fractions, and as a date separator.
These operators are mathematical operators and both have different uses. / Only perform the division operation in mathematics and returns results as the quotient. While % is known as modulus. / divides and returns the answer.
Answer: First of all, / is an assignment operator and // is a comparison operator. / operator is used to assign value to a variable and //operator is used to compare two-variable or constants.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With