0
I do not understand the difference between// and % do anyone give clatity please with example.
3 Answers
+ 2
So, basically, the / (division operator) returns the amount of times a number can "fit" into another number. For example, 9 / 4 returns 2, because you can only "fit" 2 fours in 9. (2X4 = 8 < 9) and not three because (3X4 = 12 > 9). The other one, the modulus operator (%) returns the REST of the division of a number by another one. For example 9 % 4 = 1 because 2*4 = 8 and 9-8 = 1.
So: 9 % 4 = ((9) - ((9 / 4) x 4))
or, generally:
x % y = ((x) - ((x / y) Ă y))
+ 1
// is floor division, where the fraction in the answer is dropped. Ex. 9 // 4 = 2 (the fraction .25 was dropped) . % is modulus that gives the remainder. So, 9 % 4 = 1
0
% is called modulus (aka remainder operation). Lets say you are dividing x by y. If x / y can return an integer, x % y will be 0. (I.e. 4 / 2 =2. 4%2 = 0). If x / y does not return an integer (ie. 5/2 = 2.5). X % y will return the remainder of the division. If you are using Python 3, // is floor division.
X // y will divide x by y and then return the answer rounded down to the lowest integer.