What does % mean


#1



For example in 108 % 100 what would be the outcome?


Replace this line with your code.


#2

% - Its called modulo operator in python.

It gives remainder.

108 % 100 will return 8.

On the other hand...

/ - will give quotient.
108 / 100 will return 1.


#3

@greentreemee is correct. I'm not going to take away from his answer. What I am going to do is just go into more detail (for my own learning and hopefully for your benefit).

The % is the "modulo." If you were to have the following examples:

1 % 4 it would = 1. Adding more, 2 % 4 = 2, and 3 % 4 = 3. The denominator is unable to go into 1, 2, and 3, so the numerator is the result.

However, if it was 4 % 4, that would = 0. Why? Because the 4 can go into it one (1) time. So how many is left over? 0. This holds true for 8 % 4 = 0, 12 % 4 = 0, 16 % 4 = 0, and whatever else can go into the denominator by 4.

Finally, if the statement was 5 % 4, then the result would = 1. Why? Because 4 can go into 5 one (1) time, and the result left over is 1. 6 % 4 = 2, 7 % 4 = 3, but then it returns back to 0 after we reach 8 % 4, and that was explained in the above paragraph. So what happens when it reaches 9 % 4? It = 1, and the process repeats itself.

Hope that helps!


#4

Only in Python 2. In Real terms, it is 1.08, as would be reflected in a Python 3 quotient. A float.


#5

Yup!As I have answered wrt CC python's version! :slight_smile:


#6

Yes, but also without proviso so it becomes a sort of blanket statement. Do the math.

Edit

Do the math

Metaphorically speaking.


#7

Noted! from now I'll consider mentioning both versions in need! :thumbsup:


#10