I want an explanation about the following expression(in description) which uses floor division, modulo, and comparison operators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

I want an explanation about the following expression(in description) which uses floor division, modulo, and comparison operators

print((-9//4)==(-(9//4+1)) and (-9%4)==(4-9%4)) What is the output? Also, help me to understand it better.

28th May 2020, 1:24 PM
Eshan Gayanga
Eshan Gayanga - avatar
13 Answers
+ 7
// is python floor division which always returns Quecent (for float value it returns smaller one. Example- >>9//4 2 And >>>-9/4=-2.25 >>>-9//4 -3(smaller than -2. 25) Python modulo operator always return the remainder having the same sign as the divisor. >>> -5 % 3 1 >>> 5 % -3 -1 >>> -5 % 3 = (1 -2*3) % 3 = 1 5 % -3 = (-1 * -2*-3) % 3 = -1 Similarly >>>-9%4=(3-4*3) %4=3 Hope it's clear now👍
28th May 2020, 1:50 PM
Shobhit Pandey
Shobhit Pandey - avatar
+ 7
// is floor division -9//4 = -2.25 =-3 (-(9//4+1)) Here 9//4=2.25=2 So -(2+1)=-3 -3==-3 so here 2.25 is rounded to value smaller than it which is 2 because of floor division but in -2.25 it is rounded to -3 as -3 is smaller than -2.25 and sorry I don't have any good explanation for (-9%4) ,but you have already got an answer from others
28th May 2020, 1:55 PM
Abhay
Abhay - avatar
+ 7
print((-9//4)==(-(9//4+1)) and (-9%4)==(4-9%4)) # -9//4 => -3 # -(9//4+1) => -(2+1) => -3 # -9%4 => 3 # 4-9%4 => 4-1 => 3 # print((-3 == -3) and (3 == 3)) => print(True and True) => print(True)
28th May 2020, 3:32 PM
Jenson Y
+ 7
Lay_in_life thanks sir👍
28th May 2020, 3:35 PM
Eshan Gayanga
Eshan Gayanga - avatar
+ 6
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 you didn't explained how? As he can check the output by running it in code playground itself ,
28th May 2020, 1:38 PM
Abhay
Abhay - avatar
+ 6
Why is the third expression 3? 🤔
28th May 2020, 1:41 PM
HonFu
HonFu - avatar
+ 6
👍Clear now.🙂Thank you everyone!
28th May 2020, 1:52 PM
Eshan Gayanga
Eshan Gayanga - avatar
28th May 2020, 2:14 PM
Abhay
Abhay - avatar
+ 5
Please why -9//4=(-(9//4+1))
28th May 2020, 1:39 PM
Eshan Gayanga
Eshan Gayanga - avatar
+ 4
I think his doubts was How (-9%4) =3 And no one is giving explanation about it,
28th May 2020, 2:00 PM
Shobhit Pandey
Shobhit Pandey - avatar
+ 4
Ok.thanks sir.
28th May 2020, 2:11 PM
Eshan Gayanga
Eshan Gayanga - avatar
+ 2
thanks again I will have a better way for a
4th Jun 2020, 11:11 AM
Samantha Acom
Samantha Acom - avatar
0
Pada Oktober
30th May 2020, 8:34 AM
Dede Sarip
Dede Sarip - avatar