Difference between "//" and "/" in Python 2 - Stack Overflow
What is the output of the following code in Python programming?
7%2
Group of answer choices
3
Both 1 and 3
1
None of these answers.
What is the output of the following code in python? 7/2
How come -7 mod 3 is 2 in Python/Haskell, yet -1 in most other programming languages like Java/C?
Videos
In Python 2.x, the default division operator is "Classic division". This means that /, when used with integer operators will result in integer division similar to C++ or java [i.e. 4/3 = 1].
In Python 3.x, this is changed. There, / refers to "True division" [4/3 = 1.3333..], whereas // is used to request "Classic/Floor division".
If you want enable "True division" in Python 2.7, you can use from __future__ import division in your code.
Source: PEP 238
For example:
Copy>>> 4/3
1
>>> 4//3
1
>>> from __future__ import division
>>> 4/3
1.3333333333333333
>>> 4//3
1
The difference occurs in case of Python 3.x .
In Python 3.0, 7 / 2 will return 3.5 and 7 // 2 will return 3. The operator / is floating point division, and the operator // is floor division or integer division.
But in case of Python 2.x there won't be any difference and the text is wrong I believe, here is the output I am getting.
CopyPython 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.
>>> 4/2
2
>>> 2/4
0
>>> 5//4
1
>>> 2//4
0
>>>
// is the floored-division operator in Python. The difference is visible when dividing floating point values.
In Python2, dividing two ints uses integer division, which ends up getting you the same thing as floored division. However, you can still use // to get a floored result of floating point division.
# Python 2
>>> 10.0 / 3
3.3333333333333335
>>> 10.0 // 3
3.0
However, in Python3, dividing two ints results in a float, but using // acts as integer division.
# Python3
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3
If you are (still) working in Python2, but want to someday convert to Python3, you should always use // when dividing two ints, or use from __future__ import division to get the Python3 behavior in Python2
Floored division means round towards negative infinity. This is the same as truncation for positive values, but not for negative. 3.3 rounds down to 3, but -3.3 rounds down to -4.
# Python3
>>> -10 // 3
-4
>>> 10 // 3
3
In python 2.7, to do real division you'll need to import division from a module named future:
from __future__ import division
Then, the / will be the real (floating) division, i.e.:
15 / 4 => 3.75
And the // will be the integer division (the integer part of the real division), i.e.:
15 // 4 => 3
In most programming languages like C, Rust, Java, JavaScript -7 % 3 results in -1, this makes sense to me logically since if I have "negative 7 dollars" divided it across three people, each will get "-2 negative dollars" and "-1 negative dollar" will remain.
So how come in any calculators, and the few mathematics-friendly programming languages like Python and Haskell, -7 % 3 results in 2? Like logically speaking how could dividing a negative number result in a positive number, and where did the 2 even came from, from a logical standpoint?