0

Difference between / and //

6th Nov 2016, 5:17 PM
ansu
3 Answers
+ 3
In Python 3.0, 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 behavior of 3.0 Regardless of the future import, 5.0 // 2will return 2.0 since that's the floor division result of the operation. Here are the changes 2.2 introduces: A new operator, //, is the floor division operator. (Yes, we know it looks like C++’s comment symbol.) // always performs floor division no matter what the types of its operands are, so 1 // 2 is 0 and 1.0 // 2.0 is also 0.0. // is always available in Python 2.2; you don’t need to enable it using a __future__statement. Also see the source for more information : https://docs.python.org/3/whatsnew/2.2.html#pep-238-changing-the-division-operator
6th Nov 2016, 5:51 PM
Shady Alset
Shady Alset - avatar
0
5/2 = 2.5.... normal division 5//2 = 2.... division, but gets rid of remainder
6th Nov 2016, 9:50 PM
Josh Maloy
Josh Maloy - avatar