What does % do in Python?
23May, 2022
How Can We Help?
Created On
byMatt
Print
You are here:
< Back
Maker of websites,
Teacher,
Cat enthusiast
Teacher,
Cat enthusiast
Latest posts by Matt (see all)
- The 2022 End of Year Website Update - 01/01/2023
- Celebrating Ada Lovelace day - 10/10/2022
- Website update 5/9/22 - 05/09/2022
In Python % is the modulo operator and can be used for calculating the remainder of an equation.
For example:
print(12 % 5)
The answer that would be outputted to the screen is 2 because 12 can be divided by 5 twice (5 x 2 = 10) and the remainder of this calculation is 2 (12-10) which is the answer.
Another example:
print(9 % 3)
The answer outputted to the screen is 0 because 9 can be divided by 3, 3 times with a remainder of 0. As there is no remainder for this calculation 0 is returned.
Matt
administrator
Maker of websites,
Teacher,
Cat enthusiast