Question
What does the % mean in this list comprehension example?
Answer
In Python the %
symbol is the modulo operator and returns the remainder after division is performed.
If we do 10 % 2
, we get 0
as the result, since 10 / 2 leaves no remainder.
But if we do 10 % 3
we get 1
because 3 goes into 10 3 times with a remainder of 1.
A common use of modulo is checking whether a number is divisible by something by doing if number % number == 0:
.