The // operator is floor division:it truncates the result down to its floor.
The floor means the closest whole number below the true result.
The effect is to round down, not strictly truncate, and this matters for negatives.
import math print( math.floor(2.5) ) # Closest number below value print( math.floor(-2.5) ) print( math.trunc(2.5) ) # Truncate fractional part (toward zero) print( math.trunc(-2.5) )
When running division operators, you only truncate for positive results, since truncation is the same as floor.
For negatives, it's a floor result.
They are both floor, but floor is the same as truncation for positives.
Here's the case for 3.X:
c:\python33\python print( 5 / 2, 5 / -2 ) (2.5, -2.5) print( 5 // 2, 5 // -2 ) # Truncates to floor: rounds to first lower integer (2, -3) # 2.5 becomes 2, -2.5 becomes -3 print( 5 / 2.0, 5 / -2.0 ) (2.5, -2.5) print( 5 // 2.0, 5 // -2.0 ) # Ditto for floats, though result is float too (2.0, -3.0)
The 2.X case is similar, but / results differ again:
c:\python27\python print( 5 / 2, 5 / -2 ) # Differs in 3.X (2, -3) print( 5 // 2, 5 // -2 ) # This and the rest are the same in 2.X and 3.X (2, -3) print( 5 / 2.0, 5 / -2.0 ) (2.5, -2.5) print( 5 // 2.0, 5 // -2.0 ) (2.0, -3.0)