Reputation: 1225
In python
3//2
evaluates to 1
, in this case, the fractional part is discarded
while 1.2//1
evaluates to 1.0
Upvotes: -1
Views: 116
Reputation: 1509
The //
operator performs floor division and will return the largest multiple of the divisor that is les than or equal to the dividend.
E.g. 3 // 2
will return 1
because 3
divided by 2
is 1.5
and the fractional part is cut off.
On the other hand 1.2 // 1
will return 1.0
because 1.2
divided by 1
is 1.2
, and the result is not an integer. In this case the //
operator would return a float number with the fractional part preserved.
Note: the //
operator has different behavior depending on the types of the operands. If both operands are integers, the result is an integer. If one operand is a floating-point number, the result is a floating-point number.
E.g.
>>> 3.0 // 2
1.0
>>> 3 // 2.0
1.0
>>> 4 // 2.0
2.0
>>> 4 // 2
2
Upvotes: 0