What does this 108 % 100 mean?


In previous lessons % was a variable. Now it seems like it is some kind of math operation that was not explained anywhere. if you do a google search for % python you will see others had the same issue.
In the replies I read they called it a modulo (some kind of math operation).

So how can it be a variable and a math operator at the same time?


In math it shows the remainder of the division of two numbers. So for example:

The 8 is the remainder of those two numbers divided.


I still don’t understand what operation are you performing?

Is it 108 divided by 100 or the other way around? Other way around I get 9,2 something.


And why it is used as a variable a few lessons back?


the modulo operator calculates the remainder. What is left after the biggest possible division.

we can use the table of 100:

100 * 1 < 108 # true
100 * 2 < 108 # false

so the biggest possible division is 100. To get the remainder we do the number minus the biggest possible division: 108 - 100 = 8

it can’t be used as a variable, i can be use like for string fomatting:

x = "world"
print "hello %s" % x

just like we write 5x3 in math, we can also use x in a word. What is used for depends on the context. Same here, we can use % for different purposes, python will figure it out :slight_smile:


Thanks this helps …!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.