+ 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.
18 ответов
+ 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👍
+ 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
+ 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)
+ 7
Lay_in_life thanks sir👍
+ 6
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 you didn't explained how? As he can check the output by running it in code playground itself ,
+ 6
Why is the third expression 3? 🤔
+ 6
👍Clear now.🙂Thank you everyone!
+ 6
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 thks
+ 5
Please why -9//4=(-(9//4+1))
+ 4
I think his doubts was
How (-9%4) =3
And no one is giving explanation about it,
+ 4
Ok.thanks sir.
+ 2
thanks again I will have a better way for a
0
Pada Oktober