Why output is -5.🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why output is -5.🤔

print(-9//2) #output=-5 but why,can you explain me this.

9th May 2018, 4:17 PM
Maninder $ingh
Maninder $ingh - avatar
2 Answers
+ 4
The // operator explicitly floors the result. Floors means that it returns the largest integer less than or equal to the result. In your case, the implicit result through classic division is -4.5, which is floored to -5 (as it is smaller than the result). Works same as the math.floor() function, normally.
9th May 2018, 4:42 PM
Dev
Dev - avatar
+ 1
Maninder Singh, As -9 isn't divisible by 2 so, it will try to find a number "less than" -9 that is divisible by 2 i.e., it will move towards left on the real number line. And, there it will catch -10, which is surely divisible by 2. So, -9 // 2 = (-9-1) // 2 = -5 That makes your answer!
9th May 2018, 5:10 PM
777
777 - avatar