Python Why // gives different (absolute) value when applied on negative numbers versus positive

Viewed 78

Sorry if the question is too trivial, what's the difference:

Code

print(14//3)  = 4 
print(-14//3) =-5 
1 Answers

The // is floor division in Python therefore the difference:

 14//3 = floor( 4.6666) =  4
-14//3 = floor(-4.6666) = -5

As the docs state:

The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result. Division by zero raises the ZeroDivisionError exception.

Related