0
I don't get how % work like in X=9 X%=2 Print (X) It shows 1 Like how?
14 Antworten
+ 6
Hi Ba
In modulus operator its divide with whole positive number which is 4 in case of 9 % 2
So 2 * 4 + 1 = 9
So here 1 is a reminder value
9 / 2 = 4.5
9 // 2 = 4
9 % 2 = 1
+ 4
9 divided by 2 ---> remainder 1
+ 2
Hi Ba
You are trying to solve for the remainder value so think of it as long division.
How many times does 2 go into 9? 4 times, which would be 8. Then you take 9 - 8 which gives you your remainder value of 1.
You want your remainder value to be an int (a whole number) and not a float (a decimal point).
+ 1
Remainder of what ?
And 9%2 is it multiplied or what?
+ 1
9 divided by 2 isn't 4,5?
from where comes 1? I really don't get it 😅
+ 1
The modulo operator evaluates for the closest possible whole number and returns the remainder.
The code:
X %= 2
Is reassigning the value of x. It can also be written as:
X = x % 2
So it stores the new value of x after the modulo operation is completed.
Modulo operation is properly explained in the "python for beginners course" .
+ 1
X%=2 is same as x=x%2
9%2=1 .1 is the remainder on dividing 9 by 2
0
Because modulo operator gives remainder: 9=2×4+1
0
% shows a division remainder. So 9 : 2 = 4(1 - remainder)
0
% means remainder. So you divide the 9 by2 for 4. But what is left? What does the 2 not divide into? The one, since 9 is odd. So it outputs one. If it were 2/2, % would produce 0, since 2 goes into 2 evenly. It is divided but it only ourputs whatever is
0
It is the remainder of it divided by 2
0
modulus means division with reminder so when you devide 9 by 2 the result will be 4 and 1 as the reminder and if you devide 8 by 2 the result will be 4 and 0 is the reminder